6

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?

2 Answers 2

8

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure why I am still getting the Call to function on non object error, my code pastebin.com/z07phdHj. The strange thing is when I try echo get_class($this->todo) i get Zend_View
Can you try setting it in your Bootstrap or similar, it could be that your telling it to use todo for objects after the partialLoop as run?
I think it will be a better idea to set it just b4 echo $this->partialLoop() as its not a general app thing. Thanks anyway
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.