Mirmo Dynamics

Si tu kiffes pas reunoi, t'écoutes pas et puis c'est tout.

To content | To menu | To search

Keyword - view helper

Entries feed - Comments feed

Sunday 15 July 2007

Pagination with the Zend Framework

Yesterday I came up with a small pagination component for the Zend Frameworks. It implements the Proxy pattern around a Zend_Db_Table object, and overloads the fetchAll method. The main problem I encountered here was to retrieve the total number of rows for the table. I'm using a Zend_Db_Select query for now, but I'll have to improve that. The component also features a view helper to draw the pagination links.

You'll find the code for the component and the view helper on my SVN.

And here is how it is used in the controller:

    public function indexAction() {
        $urls = new Riskle_Db_Table_Paginate(new Urls, $this->_getParam('page'));
        $this->view->urlsList = $urls->fetchAll(null, 'datetime DESC');
        $this->view->paginationInfos = $urls->getPaginationInfos();
    }

The view helper takes paginationInfos as an argument:

echo $this->paginate($this->paginationInfos);

UPDATE

As pointed out by Guy, the _getPageCount method does not actually takes care of the $where condition, thus rendering the class inefficient as getting the real totel number of items. This issue will be adressed in an upcoming version of the class :-)

UPDATE

There's an updated version of this component available.

Sunday 1 July 2007

Get the current $view from a view helper

So you're making your own view helper and you need, for a reason, to access the running $view instance. Don't worry, all you have to do is implement a setView() method which will be called on your helper's instantiation, with the $view as argument:

class My_View_Helper_SpecialPurpose {
	
	protected $_view = null;
	
	public function setView($view) {
		$this->_view = $view;
	}

}

And voila !