0
我在Module.php這樣來配置我的模型:的Zend 2:使用分頁程序與TableGateway
'MyModule\Model\MyTable' => function($sm) {
$tableGateway = $sm->get('MyTableGateway');
$table = new MyTable($tableGateway);
return $table;
},
'MyTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(
new Ticket());
return new TableGateway('someTable', $dbAdapter, null,
$resultSetPrototype);
},
現在我這樣做是在MyTable.php:
$resultSet = $this->tableGateway->select(
function(Select $select) {
$select
->order('id ASC')
->where->equalTo('aaaa', 2343)
->and
->where->notEqualTo('bbbb', 'sdfsdf')
;
});
現在我想使用分頁,但看zend tutorial他們有你這樣做:
// create a new Select object for the table album
$select = new Select('someTable');
// create a new result set based on the Album entity
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
// create a new pagination adapter object
$paginatorAdapter = new DbSelect(
// our configured select object
$select,
// the adapter to run it against
$this->tableGateway->getAdapter(),
// the result set to hydrate
$resultSetPrototype
);
$paginator = new Paginator($paginatorAdapter);
return $paginator;
我爲什麼必須這樣做$select = new Select('someTable');
這是否違反DI原則,因爲我靜態輸入表名?在Module.php中配置表名並且仍然必須在實際模型中對其進行硬編碼似乎是正確的。我能否以某種方式從服務管理器中檢索表名?
'$ select = new Select($ this-> tableGateway-> getTable());'? – Crisp
太好了。我也在模型中設置resultSetPrototype,是否有一個爲什麼只從Module.php中取出它? – red888
不要緊,我認爲getResultSetPrototype()是我想要的 – red888