Click Fraud Protection
PHP related blog, freelancer to hire!

Zend Paginator – how to access objects, not data only

If you’re using Zend_Paginator, I’m sure on some point you’ll need to access your object instead of only data.


First of all you need to have your Zend_Db_Table defined with rowchild, for example if your table class is App_Model_Db_Books, add this variable

protected $_rowClass = 'App_Model_Db_Books_Row';

This will ensure that every object in your rowset will be instance of that class


Then inside App_Model_Db_Books_Row define your method f.e.:

public function getCountOfImages() {
////// your code here


All right so we have our Table class, row class, now time to use Zend_Paginator


In your controller do:

$bookTable = new App_Model_Db_Books();
$selectForPaginator = $bookTable->select(); // this will prepare query for paginator, so you can modify it as you need
$this->view->paginator = Zend_Paginator::factory($selectForPaginator);
$this->view->partialLoop()->setObjectKey('model'); // this will do the trick to tell partial that we want to use objects, and name of the variable in partial will be 'model'


So now in your script view template you can use your paginator object with helper

<?php echo $this->partialLoop('paginator-partial.phtml', $this->paginator); ?>


And then finally in partial template paginator-partial.phtml you have your model variable:

<?php echo $this->model->getCountOfImages() ?>

or you can use any of the methods defined in App_Model_Db_Books_Row


Useful links:


Leave a Reply