Cakephp 1.3:在分页评论中显示用户数据

发布于 2024-12-11 12:30:06 字数 750 浏览 0 评论 0原文

我想这是另一个相当基本的问题,但我觉得我在兜圈子,没有想法,哈哈。

我正在尝试创建一个带有分页的评论系统(通过 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 技术交流群。

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

发布评论

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

评论(2

我不会写诗 2024-12-18 12:30:06

如果我没记错的话

'contain' => array('Entry', 'User.avatar,User.username')),

应该可以解决问题

if I remember correctly

'contain' => array('Entry', 'User.avatar,User.username')),

should do the trick

晨敛清荷 2024-12-18 12:30:06

好吧,我解决了......

我只需将正确的外键添加到我的评论模型中,即:

var $belongsTo = array(
'Entry' => array('className' => 'Entry', 'foreignKey' => 'page_id'),
'User' => array('className' => 'User', 'foreignKey' => 'user_id'),
);

现在它终于获取了适当的用户信息!

Okay, I solved it...

I just had to add the proper foreignKey to my Comment model, i.e:

var $belongsTo = array(
'Entry' => array('className' => 'Entry', 'foreignKey' => 'page_id'),
'User' => array('className' => 'User', 'foreignKey' => 'user_id'),
);

Now it finally fetches the appropriate user information!

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