Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:16:34+00:00 2026-06-12T09:16:34+00:00

How to use the database mapper with the paginator? I’m having a bit trouble

  • 0

How to use the database mapper with the paginator?

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).

From what I can tell it’s not as straight forward as I would have hoped. DbSelect is expecting a Zend\Db\Sql\Select and an adapter. The adapter is a non issue and can be retrieved with:

$this->newsContents()->getAdapter()

but I’m having trouble getting a Select object out from my TableGateway without duplicating my query code. Is there an easy way to solve this problem?

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;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:16:35+00:00Added an answer on June 12, 2026 at 9:16 am

    Implementing paginator with DbSelect as the adapter

    // controller
    public function articleAction()
    {
        //...
        $paginator = $this->newsContents()->getPages($this->articleId());
        $paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
    
        return array(
            'css'       => 'news.css',
            'article'   => $article,
            'paginator' => $paginator,
        );
    }
    ?>
    
    // table
    public function getPages($newsId)
    {
        $sql = $this->getSql();
        $select = $sql->select();
        $select->where(array('news_id' => $newsId))->order('id ASC');
        $adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);
        $paginator = new \Zend\Paginator\Paginator($adapter);
        return $paginator;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some application on RoR with Mongodb database. I use Mongoid mapper. Model
Can i use the SqlAlchemy ORM-Mapper to only generate the SQL-Code? With simple tables
MySQL uses the USE database_name to change the active database. Does that work on
When is it best to use database as a session store in PHP? I
I use H2 Database as DBMS from a remote computer,so I enabled remote access
when we use sqlLite database for our projects does the path of .db file
I have to use oracle database in android. I have tried to work as
I want to use Access database with query for pivot table in Excel. How
What components can I use to use SQlite database with Delphi 6?
I'd like to use object database to persist some classes created in IronPython. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.