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 :

About these ads

10 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 */);

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


Get every new post delivered to your Inbox.

Join 219 other followers

%d bloggers like this: