当前位置: 动力学知识库 > 问答 > 编程问答 >

php - relation using BELONGS_TO with user table

问题描述:

I'm trying to join my comment table with my user table like this comment.userId=user.id

unfortunately when i print_r($this->user); i get nothing. what am i doing wrong here?

in my comment model

 public function relations()

{

return array(

'user' => array(self::BELONGS_TO, $this->module->user, 'userId'),

);

}

public function getLastName()

{

print_r($this->user);

die;

return is_null($this->user) ? '' : $this->user->{$this->module->lastNameAttribute};

}

where

$this->module->user = 'User'; //User is the model name

and

$this->module->lastNameAttribute = 'last_name';

in my view

$comments = $model->getCommentDataProvider();

$comments->setPagination(false);

$this->widget('zii.widgets.CListView', array(

'dataProvider'=>$comments,

'itemView'=>'application.modules.comment.views.comment._view', //view file location

'emptyText' => '<div class="alert alert-info">No comments yet.</div>',

'summaryText' => '<h4>'.Yii::t('commentTitle','{n} comment|{n} comments',$comments->totalItemCount).'</h4>'

));

网友答案:

I see a small typo, maybe you mistaken while making a post:

comment.userid=user.id

here it's userid and in relation you referenced it with userId

check it out please

EDIT - after question edit

I'm not familiar with CommentableBehavior but it seems to me that you need to eager load User model with each Comment:

$comments = Yii::createComponent($this->module->commentModelClass)->with('user')->findAll($this->getCommentCriteria());

I added with('user') in getComments() method

分享给朋友:
您可能感兴趣的文章:
随机阅读: