Thursday 23 May 2013

Zend Framework 2 : Paginator – Using TableGateway Object


Zend 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 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.

No comments:

Post a Comment