简体   繁体   中英

Cakephp 1.3: Displaying user data in paginated comments

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!

if I remember correctly

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

should do the trick

Okay, I solved it...

I just had to add the proper foreignKey to my Comment model, ie:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM