Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework 2 Paginator + TableGateway
    primarykey
    data
    text
    <h2>How to use the database mapper with the paginator?</h2> <p>I'm having a bit trouble understanding how I implement the DbSelect paginator using the code below (right now it's using the Iterator adapter which doesn't work for ResultSets). </p> <p>From what I can tell it's not as straight forward as I would have hoped. DbSelect is expecting a <code>Zend\Db\Sql\Select</code> and an adapter. The adapter is a non issue and can be retrieved with:</p> <pre><code>$this-&gt;newsContents()-&gt;getAdapter() </code></pre> <p>but I'm having trouble getting a <code>Select</code> object out from my TableGateway without duplicating my query code. Is there an easy way to solve this problem? </p> <p><strong>NewsController.php</strong></p> <pre><code>&lt;?php namespace News\Controller; use Zend\Paginator\Paginator; class NewsController extends \Application\Controller\WebsiteController { protected $newsTable; protected $newsContents; protected function newsTable() { return $this-&gt;getServiceLocator()-&gt;get('News\Model\NewsTable'); } protected function newsContents() { return $this-&gt;getServiceLocator()-&gt;get('News\Model\NewsContentsTable'); } protected function articleId() { return (int) $this-&gt;params()-&gt;fromRoute('id'); } public function articleAction() { $article = $this-&gt;newsTable()-&gt;getArticle($this-&gt;articleId()); $pages = $this-&gt;newsContents()-&gt;getPages($this-&gt;articleId()); $paginator = new Paginator(new \Zend\Paginator\Adapter\Iterator($pages)); $paginator-&gt;setCurrentPageNumber($this-&gt;params()-&gt;fromRoute('page')); return array( 'css' =&gt; 'news.css', 'article' =&gt; $article, 'paginator' =&gt; $paginator, ); } } </code></pre> <p><strong>NewsContentsTable.php</strong></p> <pre><code>&lt;?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-&gt;adapter = $adapter; $this-&gt;resultSetPrototype = new ResultSet; $this-&gt;resultSetPrototype-&gt;setArrayObjectPrototype(new NewsContents); $this-&gt;initialize(); } public function getPages($newsId) { $rowset = $this-&gt;select(function(Select $select) use ($newsId) { $select -&gt;order('order ASC') -&gt;where(array('news_id' =&gt; $newsId)); }); return $rowset; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload