Cakephp 1.3:在分页评论中显示用户数据
我想这是另一个相当基本的问题,但我觉得我在兜圈子,没有想法,哈哈。
我正在尝试创建一个带有分页的评论系统(通过 Ajax),它必须能够显示撰写特定评论的用户的姓名、头像等。听起来很简单,对吧?
好吧,一切工作正常,除了到目前为止我根本无法找到一种方法来检索相应的用户信息,而且我也无法在文档中找到任何有用的信息。
到目前为止,这是我的分页代码:
$this->paginate['Comment'] = array(
'conditions'=>array('Entry.id'=>$id),
'contain' => array('Entry', 'User'=>array('avatar', 'username') ),
'limit' => 10
);
$comments = $this->paginate('Comment');
$this->set(compact('comments'));
所以我使用 contains 来获取用户模型的数据,我尝试将其显示在我的视图中,如下所示:
echo $comment['User']['username'];
echo $comment['User']['avatar'];
但是这样,它当然会显示与 $id 对应的用户的信息...
但是,我需要通过当前评论的外键 user_id 获取用户信息。目前我不知道该怎么做...任何帮助将不胜感激。 提前致谢!
another fairly basic question I suppose, but I feel like I'm running in circles and out of ideas, haha.
I'm trying to create a commenting system with pagination (via Ajax), which has to be able to display the name, avatar, etc. of the user that wrote a particular comment. Sounds simple enough, right?
Well, everything works fine, except so far I simply wasn't able to find a way to retrieve the corresponding users information and I couldn't find anything helpful in the docs either.
Here's my pagination code so far:
$this->paginate['Comment'] = array(
'conditions'=>array('Entry.id'=>$id),
'contain' => array('Entry', 'User'=>array('avatar', 'username') ),
'limit' => 10
);
$comments = $this->paginate('Comment');
$this->set(compact('comments'));
So I've used contain to get the data of the user model, which I try to display in my view like this:
echo $comment['User']['username'];
echo $comment['User']['avatar'];
But that way, it of course displays the information of the user corresponding to $id...
However, I need to get a users info through the foreignkey user_id of the current comment. And at the moment I'm at a loss how to do that... Any help would be greatly appreciated.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话
应该可以解决问题
if I remember correctly
should do the trick
好吧,我解决了......
我只需将正确的外键添加到我的评论模型中,即:
现在它终于获取了适当的用户信息!
Okay, I solved it...
I just had to add the proper foreignKey to my Comment model, i.e:
Now it finally fetches the appropriate user information!