views:

280

answers:

2

Is it possible to use the findParentRow() method from within a Zend_Paginator Object? I'm trying some code that works fine on an object returned by fetchAll from a DB resultset and works fine. With the Zend_Paginator object it doesnt work though.

In my controller i have:

public function downloadedAction()
{
    $images = new Model_ApplicationImages();
    $paginator = $images->fetchPaginated();
    $paginator->setCurrentPageNumber($this->_getParam('page'));
    $this->view->paginator = $paginator;
}

In my model i have:

public function fetchPaginated()
{
    $select = $this->select()
                   ->from($this->_name)
                   ->where('status = ?','approved')
                   ->where('downloaded = ?','0');
    $adapter = new Zend_Paginator_Adapter_DbSelect($select);
    $paginator = new Zend_Paginator($adapter);
    $paginator->setItemCountPerPage(10);
    return $paginator;
}

In my view i have:

    $this->partialLoop()->setObjectKey('paginator');
    echo $this->partialLoop('admin/list-downloaded.phtml', $this->paginator); 

and in the partial:

    $this->paginator->findParentRow('Model_Application')->name

It appears though that the object key is not being used or not being set properly as within the partial var_dump($this->paginator) is NULL and the other values being passed from the paginator are there but under $this->key and not $this->paginator->key as they should be

+1  A: 

Actually the sample code you provided is quite correct, I have very similar setup, albeit I used Zend_Paginator_Adapter_DbTableSelect as an adapter. So, it is definitely possible in principle.

It is worth checking what is returned from fetchPaginated(), in your controller action.

The other thing probably worth checking, is whether $view->paginator doesn't get overwritten in your view script (which is a bad thing for separate reasons). Plus, you can actually play with object key name, use "model" for example instead of "paginator" in your setObjectKey().

P.S. Not sure whether it is relevant or not, but I setup the object key in controller:

$this->view->partialLoop()->setObjectKey('model');
$this->view->partial()->setObjectKey('model');
Victor Farazdagi
+2  A: 

setObjectKey() only works in partials, not in partial loops.

A PartialLoop essentially runs a Partial for each element in the array or Traversable object passed to it. So by the time it gets to your partial view script, you're no longer working with the Paginator object, but with its paginated contents.

To be more precise, in a partial loop, the setObjectKey() works at the partial level too. So you could pass an array of objects to a partial loop and in the partial each object will then be accessible through the object key. I presume in your case you're getting NULL out of the paginator key, because your paginator elements are returned as arrays, so there isn't any object to be put in the object key.

If you need access to the Paginator itself, you should use a partial instead, for which setObjectKey() does work the way you expect, and loop over the elements yourself.

I suggest you keep the source code of the Zend Framework handy when something doesn't work the way you expect. Sadly, I've had more success figuring out how to use it by reading through the code than by reading through the documentation.

mercator