TableGateway 能否使用多个表 Zend Framework 2
Can TableGateway Use Multiple Tables Zend Framework 2
在我的 module.php 文件中,我想通过 Zend Framework 中的 TableGateway class 传递多个 table 名称,但我找不到任何关于它的文档,除了它仅限于一个 table。这个 (TableGateway) class 的 phpdoc 说可以传递一个数组,但是我不确定它是否接受多个 table。
例如 Module.php:
'Application\Model\LoginModel' => function($sm) {
$table_gateway = $sm->get('LoginService');
$table = new LoginModel($table_gateway);
return $table;
},
'LoginService' => function($sm) {
$db_adapter = $sm->get('Zend\Db\Adapter\Adapter');
$result_set_prototype = new ResultSet();
$result_set_prototype->setArrayObjectPrototype(new Login());
return new TableGateway(array('admins', 'members'), $db_adapter, null, $result_set_prototype);
}
是否可以这样做并像这样引用或绑定多个 table,或者它是否仅设计为每个实例允许一个 table?
不,不是。 Table 网关对象旨在提供一个表示数据库中 table 的对象。 Array
可以传给构造函数,但是如果传了,就会得到InvalidArgumentException
。请检查此代码
请再看一遍TableGateway
这里的文档
https://framework.zend.com/manual/2.4/en/modules/zend.db.table-gateway.html
在我的 module.php 文件中,我想通过 Zend Framework 中的 TableGateway class 传递多个 table 名称,但我找不到任何关于它的文档,除了它仅限于一个 table。这个 (TableGateway) class 的 phpdoc 说可以传递一个数组,但是我不确定它是否接受多个 table。
例如 Module.php:
'Application\Model\LoginModel' => function($sm) {
$table_gateway = $sm->get('LoginService');
$table = new LoginModel($table_gateway);
return $table;
},
'LoginService' => function($sm) {
$db_adapter = $sm->get('Zend\Db\Adapter\Adapter');
$result_set_prototype = new ResultSet();
$result_set_prototype->setArrayObjectPrototype(new Login());
return new TableGateway(array('admins', 'members'), $db_adapter, null, $result_set_prototype);
}
是否可以这样做并像这样引用或绑定多个 table,或者它是否仅设计为每个实例允许一个 table?
不,不是。 Table 网关对象旨在提供一个表示数据库中 table 的对象。 Array
可以传给构造函数,但是如果传了,就会得到InvalidArgumentException
。请检查此代码
请再看一遍TableGateway
这里的文档
https://framework.zend.com/manual/2.4/en/modules/zend.db.table-gateway.html