Zend 2:如何在当前模型中使用另一个模型中定义的方法?
Zend 2 : How to use a method defined in another model in my current model?
我尝试以不同的方式解决这个问题,但似乎没有任何效果。
我有 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(),
);
}
您可以像这样在工厂中将您的 UsersMetaTable
注入到您的 AttachmentTable
中:
'Admin\Model\AttachmentTable' => function($sm) {
$tableGateway = $sm->get('AttachmentTableGateway');
$usersMetaTable = $sm->get('Admin\Model\UsersMetaTable');
$table = new AttachmentTable($tableGateway, $usersMetaTable);
return $table;
},
然后您需要更新您的构造函数,以便将注入的服务放置到位:
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
class AttachmentTable
{
protected $tableGateway;
protected $usersMetaTable;
public function __construct(TableGateway $tableGateway, UsersMetaTable $usersMetaTable)
{
$this->tableGateway = $tableGateway;
$this->usersMetaTable = $usersMetaTable;
}
// ...remaining class code
}
我尝试以不同的方式解决这个问题,但似乎没有任何效果。 我有 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(),
);
}
您可以像这样在工厂中将您的 UsersMetaTable
注入到您的 AttachmentTable
中:
'Admin\Model\AttachmentTable' => function($sm) {
$tableGateway = $sm->get('AttachmentTableGateway');
$usersMetaTable = $sm->get('Admin\Model\UsersMetaTable');
$table = new AttachmentTable($tableGateway, $usersMetaTable);
return $table;
},
然后您需要更新您的构造函数,以便将注入的服务放置到位:
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
class AttachmentTable
{
protected $tableGateway;
protected $usersMetaTable;
public function __construct(TableGateway $tableGateway, UsersMetaTable $usersMetaTable)
{
$this->tableGateway = $tableGateway;
$this->usersMetaTable = $usersMetaTable;
}
// ...remaining class code
}