Zend 将变量传递给局部视图内的 PartialLoop
在我看来,我有一个包含partialLoop 的部分。 但是,当我运行该页面时,出现以下错误消息:
Call to a member function countComments() on a non-object in ...'_loop.phtml'
这是我从视图中调用部分内容的方式:
echo $this->partial('_post.phtml',$this->post);
其中 $this->post 是数据库检索的行
这是我的部分内容:
MY simplified Partial!
echo $post->countComments();//the count number is correctly output..
echo $this->partialLoop('_loop.phtml',$this->object);
这是我的partialLoop的内容:
echo $this->object->countComments();//no output!
在引导中我设置了:
$view->partial()->setObjectKey('object');
$view->partialLoop()->setObjectKey('object');
这是从partials中调用partialLoops的正确方法吗?
Ps 我在我的部分中 var_dumped $this->object ,它是一个 PostRow OBJECT。我将 var_dumped $this->object 放入 _loop.phtml 中,我有 5 个 NULL(代表 id、title、text、作者,我的帖子的日期时间字段
谢谢卢卡
)
I have in my view a partial containing a partialLoop.
But when I run the page I have the following error message:
Call to a member function countComments() on a non-object in ...'_loop.phtml'
This is how I call my partial from within my view:
echo $this->partial('_post.phtml',$this->post);
where $this->post is a DB retrieved row
This is my partial's content:
MY simplified Partial!
echo $post->countComments();//the count number is correctly output..
echo $this->partialLoop('_loop.phtml',$this->object);
This is my partialLoop's content:
echo $this->object->countComments();//no output!
In the bootstrap I have set:
$view->partial()->setObjectKey('object');
$view->partialLoop()->setObjectKey('object');
Is this the right way to call partialLoops from within partials??
P.s. I var_dumped $this->object inside my partial and it is a PostRow OBJECT.I var dumped $this->object into _loop.phtml and I have 5 NULLS (standing for id,title,text,author,datetime fields of my post)
thanks
Luca
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为原因是当您将
$this->post
传递到局部视图助手时,如下所示:局部视图助手将执行其
toArray()
方法。因此,您的$this->object
是一个数组,并且您将一个数组传递给partialLoop。因此,在您的partialLoop中,您尝试在表示行发布对象的数组上执行countComments()
,而不是实际的行对象。为了避免这种情况,我建议使用数组表示法将变量传递给partial和partialLoop视图助手,例如:
希望这会有所帮助。
I think that the reason is that when you pass
$this->post
into partial view helper like this:partial view helper will execute its
toArray()
method. Hence, your$this->object
is an array and you are passing an array to your partialLoop. So, in your partialLoop you are trying to executecountComments()
on an array representing your row post object, rather than actual row object.To avoid this, I would recommend passing variables to partial and partialLoop view helpers using array notation, e.g:
Hope this helps.
正如 Marcin 上面所说,此错误是由
partial
和partialLoop
视图助手的默认行为引起的。尽管令人困惑,但手册确实在此处进行了解释
解决方案是显式告诉局部传递对象。正如手册所解释的:
This error is caused by the default behaviour of the
partial
andpartialLoop
view helpers as Marcin said above.Although it is confusing the manual does explain this here
The solution is to explicitly tell the partial to pass the object. As the manual explains: