0
我嘗試以不同的方式解決這個問題,但似乎沒有任何工作。 我有2種型號:Zend 2:如何在我的當前模型中使用另一個模型中定義的方法?
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
class UsersMetaTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveData(UsersMeta $value)
{
...
}
public function getAttachment($user_id){
$user_id = (int) $user_id;
$rowset = $this->tableGateway->select(array('parent_id' => $user_id,'meta_key' =>'inside_users_attachement'));
$row = $rowset->current();
if (!$row) {
return false;
exit;
}
return $row->meta_value;
}
}
而且我目前的型號:
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
class AttachmentTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveData(Attachment $value)
{
...
}
public function getAttachment($user_id)
{
$user_id = (int) $user_id;
//HERE I NEED TO LOAD UsersMetaTable MODEL AND USE getAttachment METHOD
$attachment_id = $usersMetaTable->getAttachment($user_id);
if($attachment_id){
$rowset = $this->tableGateway->select(array('ID' => $attachment_id));
$row = $rowset->current();
if (!$row) {
return false;
exit;
}
return $row;
}else{
return false;
}
}
}
在AttachmentTable
模式,我需要從UsersMetaTable
模型中使用的方法getAttachment
。我如何在Zend Franework 2中做到這一點?
我的兩個模型在Modele.php
定義:
public function getServiceConfig(){
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
'Admin\Model\AttachmentTable' => function($sm) {
$tableGateway = $sm->get('AttachmentTableGateway');
$table = new AttachmentTable($tableGateway);
return $table;
},
'AttachmentTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new attachment()); // Notice what is set here
return new TableGateway('attachments', $dbAdapter, null, $resultSetPrototype);
},
'Admin\Model\UsersMetaTable' => function($sm) {
$tableGateway = $sm->get('UsersMetaTableGateway');
$table = new UsersMetaTable($tableGateway);
return $table;
},
'UsersMetaTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new UsersMeta()); // Notice what is set here
return new TableGateway('inside_users_metas', $dbAdapter, null, $resultSetPrototype);
},
),
'invokables' => array(
// defining it as invokable here, any factory will do too
'my_image_service' => 'Imagine\Gd\Imagine',
),
'services' => array(),
'shared' => array(),
);
}
Th謝謝你。它工作得很好! – Andreea