Zend 将变量传递给局部视图内的 PartialLoop

发布于 2024-11-08 05:28:49 字数 1069 浏览 1 评论 0原文

在我看来,我有一个包含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 技术交流群。

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

发布评论

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

评论(2

始终不够爱げ你 2024-11-15 05:28:49

我认为原因是当您将 $this->post 传递到局部视图助手时,如下所示:

$this->partial('_post.phtml',$this->post);

局部视图助手将执行其 toArray() 方法。因此,您的 $this->object 是一个数组,并且您将一个数组传递给partialLoop。因此,在您的partialLoop中,您尝试在表示行发布对象的数组上执行countComments(),而不是实际的行对象。

为了避免这种情况,我建议使用数组表示法将变量传递给partial和partialLoop视图助手,例如:

$this->partial('_post.phtml',array('post' => $this->post));

希望这会有所帮助。

I think that the reason is that when you pass $this->post into partial view helper like this:

$this->partial('_post.phtml',$this->post);

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 execute countComments() 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:

$this->partial('_post.phtml',array('post' => $this->post));

Hope this helps.

山人契 2024-11-15 05:28:49

正如 Marcin 上面所说,此错误是由 partialpartialLoop 视图助手的默认行为引起的。
尽管令人困惑,但手册确实在此处进行了解释

实现 toArray() 方法的对象。如果传递一个对象有一个
toArray() 方法,toArray() 的结果将被分配给
将对象视为视图变量。

解决方案是显式告诉局部传递对象。正如手册所解释的:

// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');   
// Tell partial to pass objects from partialLoop as 'model' variable 
// in final partial view script: 
$view->partialLoop()->setObjectKey('model');

这种技术在通过时特别有用
Zend_Db_Table_Rowsets 到partialLoop(),这样您就拥有完全访问权限
到视图脚本中的行对象,允许您调用
它们的方法(例如从父项或依赖项检索值)
行)。

This error is caused by the default behaviour of the partial and partialLoop view helpers as Marcin said above.
Although it is confusing the manual does explain this here

Object implementing toArray() method. If an object is passed an has a
toArray() method, the results of toArray() will be assigned to the
view object as view variables.

The solution is to explicitly tell the partial to pass the object. As the manual explains:

// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');   
// Tell partial to pass objects from partialLoop as 'model' variable 
// in final partial view script: 
$view->partialLoop()->setObjectKey('model');

This technique is particularly useful when passing
Zend_Db_Table_Rowsets to partialLoop(), as you then have full access
to your row objects within the view scripts, allowing you to call
methods on them (such as retrieving values from parent or dependent
rows).

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