I donβt know exactly how you retrieve the data, but, nevertheless, the following approach works as desired: the call chain looks like AddressResource#fetchAll(...) -> AddressService#getBar(...) -> AddressMapper#findAll(...) , and the data extraction method returns a Collection object.
AddressResource.php
... class AddressResource ... { ... public function fetchAll($params = array()) { $service = $this->getAddressService(); $collection = $service->getAddresses($params->toArray()); return $collection; } ... }
AddressService.php
... class AddressService ... { ... public function getAddresses($params = array()) { $collection = $this->getMapper()->findAll($params); return $collection; } ... }
AddressMapper.php
... class AddressMapper extends AbstractDbMapper { ... public function findAll($params = array()) { $select = $this->getSelect(); $select->where( ... ); $paginatorAdapter = $this->createPaginationAdapter($select); $collection = new AddressCollection($paginatorAdapter); return $collection; } ... }
AddressCollection.php
... use Zend\Paginator\Paginator; class AddressCollection extends Paginator { }
module.config.php
return array( ... 'zf-rest' => array( ... 'AddressBookAPI\\V1\\Rest\\Address\\Controller' => array( ... 'page_size' => 5, ... ), ... ), ... );
Now the test: I call /addresses and watching the MySQL query log:
$ tail -f /var/log/mysql_query.log ... 3 Connect root@localhost on address-book-api 3 Query SET NAMES 'UTF8' 3 Query SELECT COUNT(1) AS `C` FROM (SELECT `addresses`.* FROM `addresses`) AS `original_select` 3 Query SELECT `addresses`.* FROM `addresses` LIMIT 5 OFFSET 0 3 Quit ...
source share