Php 使用所属对象与用户表的关系
我正试图将我的Php 使用所属对象与用户表的关系,php,yii,Php,Yii,我正试图将我的comment表与我的user表连接起来,就像这样comment.userId=user.id 不幸的是,当我打印($this->user)时我什么也得不到。我做错了什么 在我的评论模型中 public function relations() { return array( 'user' => array(self::BELONGS_TO, $this->module->user, 'userId'),
comment
表与我的user
表连接起来,就像这样comment.userId=user.id
不幸的是,当我打印($this->user)时代码>我什么也得不到。我做错了什么
在我的评论模型中
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};
}
在哪里
及
在我看来
$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>'
));
$comments=$model->getCommentDataProvider();
$comments->setPagination(false);
$this->widget('zii.widgets.CListView',数组(
“dataProvider”=>$comments,
'itemView'=>'application.modules.comment.views.comment.\u view',//查看文件位置
“emptyText'=>“还没有评论。”,
“summaryText'=>”.Yii::t('commentTitle','{n}注释{n}注释',$comments->totalItemCount)。”
));
我看到一个小的打字错误,可能是你在发帖时弄错了:
comment.userid=user.id
这里是userid
,您使用userid
请检查一下
编辑-问题编辑后
我不熟悉CommentableBehavior,但在我看来,您需要用每个注释加载用户模型:
$comments = Yii::createComponent($this->module->commentModelClass)->with('user')->findAll($this->getCommentCriteria());
我在
getComments()
method中添加了with('user')
,你的意思是在这里发布整个comment模型类吗?不,只是从db加载注释的部分,我想它在你的控制器中,类似comment=comment::model()->findByPk($id)bah,没关系。把它弄明白。缺少我的findbyPK,did$comment=newcomment代码>取而代之。我的坏蛋,我很高兴你明白了
comment.userid=user.id
$comments = Yii::createComponent($this->module->commentModelClass)->with('user')->findAll($this->getCommentCriteria());