Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Zend Framework's Partial Loop with Objects

I am quite confused how to use partialLoop

Currently I use

foreach ($childrenTodos as $childTodo) {
  echo $this->partial('todos/_row.phtml', array('todo' => $childTodo));
} 

$childrenTodos is a Doctrine\ORM\PersistantCollection, $childTodo is a Application\Models\Todo

I tried doing

echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
          ->setObjectKey('Application\Models\Todo');

But in the partial when I try to access properties/functions of my Todo class, I cant seem to get them always ending up with either call to undefined method Zend_View::myFunction() when I use $this->myFunction() in the partial or if I try $this->todo->getName() I get "Call to a member function getName() on a non-object". How do I use partialLoops?

like image 709
Jiew Meng Avatar asked Jan 25 '11 14:01

Jiew Meng


2 Answers

Try this

echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
      ->setObjectKey('object');

Then in your partial you can access the object like this

$this->object

object is the name of the variable that an object will be assigned to

You can also do this once in your Bootstrap or other initialization class if you have access to the view object like so

protected function initPartialLoopObject()
{
    $this->_view->partialLoop()->setObjectKey('object');

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
    $viewRenderer->setView($this->_view);
}
like image 158
Jake N Avatar answered Nov 15 '22 09:11

Jake N


I also had "Call to function on non object" error when trying suggested syntax, seems like they've changed something on later versions of Zend Framework. The following works for me on ZF1.12:

echo $this->partialLoop()
->setObjectKey('object')
->partialLoop('todos/_row.phtml', $childrenTodos);
like image 39
Pavel Dubinin Avatar answered Nov 15 '22 10:11

Pavel Dubinin