如何将 Zend Framework 的部分循环与对象一起使用

发布于 2024-10-14 12:04:03 字数 757 浏览 6 评论 0原文

我很困惑如何使用 partialLoop

目前我使用

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

$childrenTodos 是一个 Doctrine\ORM\PersistantCollection$childTodo > 是一个 Application\Models\Todo

我尝试做

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

但是在部分中,当我尝试访问我的 Todo 类的属性/函数时,我似乎无法让它们总是以调用未定义的方法而结束Zend_View::myFunction() 当我在部分中使用 $this->myFunction() 时或者如果我尝试 $this->todo->; getName() 我得到“调用非对象上的成员函数 getName()”。如何使用partialLoops?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无力看清 2024-10-21 12:04:03

试试这个

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

然后在你的部分中你可以访问对象,就像这样

$this->object

object 是将对象分配给的变量的名称

如果你有的话,你也可以在你的 Bootstrap 或其他初始化类中执行此操作一次像这样访问视图对象

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

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
    $viewRenderer->setView($this->_view);
}

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);
}
月朦胧 2024-10-21 12:04:03

在尝试建议的语法时,我也遇到了“调用非对象上的函数”错误,似乎他们在更高版本的 Zend Framework 上更改了某些内容。以下内容对我在 ZF1.12 上有效:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文