Search code examples
zend-frameworkzend-view

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?


Solution

  • 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);
    }