paginatorでデータベースマッパーを使用するにはどうすればよいですか?
以下のコードを使用してDbSelectパギネーターを実装する方法を理解するのに少し問題があります(現在、ResultSetでは機能しないIteratorアダプターを使用しています)。
私が言えることから、それは私が望んでいたほど簡単ではありません。DbSelectはとZend\Db\Sql\Select
アダプターを期待しています。アダプターは問題ではなく、次のコマンドで取得できます。
$this->newsContents()->getAdapter()
Select
しかし、クエリコードを複製せずにTableGatewayからオブジェクトを取得するのに問題があります。この問題を解決する簡単な方法はありますか?
NewsController.php
<?php
namespace News\Controller;
use Zend\Paginator\Paginator;
class NewsController extends \Application\Controller\WebsiteController
{
protected $newsTable;
protected $newsContents;
protected function newsTable()
{
return $this->getServiceLocator()->get('News\Model\NewsTable');
}
protected function newsContents()
{
return $this->getServiceLocator()->get('News\Model\NewsContentsTable');
}
protected function articleId()
{
return (int) $this->params()->fromRoute('id');
}
public function articleAction()
{
$article = $this->newsTable()->getArticle($this->articleId());
$pages = $this->newsContents()->getPages($this->articleId());
$paginator = new Paginator(new \Zend\Paginator\Adapter\Iterator($pages));
$paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
return array(
'css' => 'news.css',
'article' => $article,
'paginator' => $paginator,
);
}
}
NewsContentsTable.php
<?php
namespace News\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;
class NewsContentsTable extends \Zend\Db\TableGateway\AbstractTableGateway
{
protected $table = 'news_contents';
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
$this->resultSetPrototype = new ResultSet;
$this->resultSetPrototype->setArrayObjectPrototype(new NewsContents);
$this->initialize();
}
public function getPages($newsId)
{
$rowset = $this->select(function(Select $select) use ($newsId)
{
$select
->order('order ASC')
->where(array('news_id' => $newsId));
});
return $rowset;
}
}