Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Paginator – Using TableGateway Object

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on May 6, 2013

zf2-zendframework2Zend Framework 2.2 is coming, more feature, more improvement.One of features that i like is DbTableGateway adapter for Paginator that can be used at your Table Class to make our life easier. The current ZF2 doc is using DbSelect Adapter, so now i will post an example how to use DbTableGateway Adapter.

I will follow the AlbumTable tutorial in the doc, so AlbumTable should be like the following :

namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Adapter\DbTableGateway;
use Zend\Paginator\Paginator;

class AlbumTable
    public function fetchAll($paginated = false)
        if ($paginated) {
            $dbTableGatewayAdapter = new DbTableGateway($this->tableGateway);
            $paginator = new Paginator($dbTableGatewayAdapter);
            return $paginator;
        return $this->tableGateway->select();

Very easy 😀

Note : currently, you can pass $where and $order to DbTableGateway adapter after tableGateway parameter.

References :

19 Responses

Subscribe to comments with RSS.

  1. ravikiran said, on May 9, 2013 at 11:08 am

    i don’t want to use tablegatewayadapter, can you help using jquery….

    • samsonasik said, on May 9, 2013 at 5:05 pm

      um…, jQuery is view area. you should learn ajax concept first

  2. zack said, on June 14, 2013 at 2:21 pm

    How can i using custom query with DbTableGateway, i don’t want to fetchAll (ex: using to search data).
    Thanks you !

  3. AbderrahimDZ24 said, on July 21, 2013 at 1:17 am

    Thank you !
    your tuto(s) is very helpfull

  4. Lom said, on October 7, 2013 at 3:46 am

    I don’t get it how to pass $order after the tablegateway parameter :-(. Hopefully you can explain once more 🙂 Big thanks!

  5. LoM said, on November 1, 2013 at 3:32 am

    just a question if its possbile, to use DbTableGateway with paginator to run as well join/groups and this stuff like in the rowset without pagination? See code below!

    Thanks a lot!

    if($paging) {
    $dbTableGatewayAdapter = new DbTableGateway($this->tableGateway, $where, $order);
    $paginator = new Paginator($dbTableGatewayAdapter);

    return $paginator;

    $rowset = $this->tableGateway->select(
    function (Select $select) use($where, $order)
    $select->join(/* some join here */);
    $select->group(/* some group here */);
    $select->where(/* some where here */);
    $select->order(/* some order here */);

      • Cristian Nores said, on July 15, 2014 at 7:34 am

        You can use this .. inside the function:

        $select = new \Zend\Db\Sql\Select ;
        ‘contenedor’ => ‘codigo_contenedor’,
        ‘id_cont’ => ‘id_cont’,


        “contenedor.id_cont = lote.id_cont”,

        ‘lote’ => ‘numero_lote’,
        ‘estado’=> ‘estado’),

        $select->where(‘fecha =’.’\”.date(‘Y-m-d’).’\”);


        $dbTableGatewayAdapter = new DbTableGateway($this->tableGateway->selectWith($select));
        $paginator = new Paginator($dbTableGatewayAdapter);

        return $paginator;

        $resultSet = $this->tableGateway->selectWith($select);

        return $resultSet;

        Just reeplace select method to selectWith($select) .

  6. Cristian Nores said, on July 15, 2014 at 8:03 am


    if ($paginated) {
    // create a new Select object for the table album

    // create a new result set based on the Album entity
    $resultSetPrototype = new ResultSet();
    $resultSetPrototype->setArrayObjectPrototype(new Contenedor());
    // create a new pagination adapter object
    $paginatorAdapter = new DbSelect(
    // our configured select object
    // the adapter to run it against
    // the result set to hydrate
    $paginator = new Paginator($paginatorAdapter);
    return $paginator;
    $resultSet = $this->tableGateway->select();
    return $resultSet;

  7. Hieu Tran said, on March 13, 2015 at 5:17 pm

    Hello Samsonasik,
    I have a trouble with paginator of zend framework 2 when I use tableGateway with join function:
    My model: I query all users in user table, and join with role table to determine role of every user:
    public function fetchAll()
    $rowset=$this->tableGateway->select(function(Select $select){
    $select->join(‘role’, ‘user.role = role.id’, array(‘label’));

    return false;

    return $rowset;
    In my controller:
    public function indexphtmlAction() {
    $userInfo = $this->getUserTable()->fetchAll();
    $page = (int)$this->params()->fromRoute(‘id’,0);
    $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($userInfo);
    $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
    return new ViewModel(array(‘userInfo’=>$paginator));
    => I can not show “label” column of role table in view.

    Other: When I return in model by $rowset->getDataSource() instead of $rowset, I got “This result is a forward only result set, calling rewind() after moving forward is not supported” error message. but when I var_dum the result, I see all necessary columns.
    Please help me see the case.

    Thanks in advance,
    Hieu Tran

  8. Nicolás Romero said, on March 12, 2017 at 1:42 pm

    Hi, im try to get data from my model i used postgres and JSONB object in POSTGRESQL, i have a model like this
    in my table model id, data->>’element’
    $statement= $this->dbAdapter->createStatement(“SELECT id, data->>’title’ AS title FROM entity LIMIT $limit OFFSET $offset”);

    $result = $statement->execute();
    $resultSetPrototype = new ResultSet();

    $resultSetPrototype->setArrayObjectPrototype(new ENTITY());

    return $resultSetPrototype;

    and in my controller want to response json

    return new JsonModel([

    ‘data’=> $paginator,


    but i dont know how to convert the result to an array for the request, can you an idea

    • samsonasik said, on March 13, 2017 at 2:19 am

      You need to echo-ed first the data you got ( should be a json like `{“title”: “Hello”, “singer”: “Adelle”}` ) then you can call `json_decode($field)` for it.

      • Nicolás Romero said, on March 19, 2017 at 12:09 am

        Thank you sam, i try to response the data but now the output is a that:

        [{“__className”:”ArrayObject”,”id”:”105″,”title”:”title 1 “…..}]

        you have any idea how to remove this item ??


        thank you for all and for rapid response

      • Nicolás Romero said, on March 19, 2017 at 12:34 am

        Sam find the solution whit json_encode($data); and the response it’s OK thank you for your time brother

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: