How to use Zend Framework's Partial Loop with Objects
Asked Answered
C

2

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?

Corcyra answered 25/1, 2011 at 14:21 Comment(0)
H
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);
}
Housecoat answered 25/1, 2011 at 14:25 Comment(3)
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_ViewCorcyra
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?Housecoat
I think it will be a better idea to set it just b4 echo $this->partialLoop() as its not a general app thing. Thanks anywayCorcyra
V
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);
Vesica answered 2/5, 2013 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.