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 :D

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

References :
http://zf2.readthedocs.org/en/latest/tutorials/tutorial.pagination.html

About these ads

12 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….
    Thanks
    ravikiran

    • 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

    Hey,
    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 ;
        $select->from(‘contenedor’);
        $select->columns(array(
        ‘contenedor’ => ‘codigo_contenedor’,
        ‘id_cont’ => ‘id_cont’,

        ));

        $select->join(‘lote’,
        “contenedor.id_cont = lote.id_cont”,
        array(

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

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

        $select->getSqlString();

        if($paginated){
        $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

    OR

    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
    $select,
    // the adapter to run it against
    $this->tableGateway->getAdapter(),
    // the result set to hydrate
    $resultSetPrototype
    );
    $paginator = new Paginator($paginatorAdapter);
    return $paginator;
    }
    $resultSet = $this->tableGateway->select();
    return $resultSet;


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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 266 other followers

%d bloggers like this: