繁体   English   中英

与用户表一起使用BELONGS_TO的关系

[英]relation using BELONGS_TO with user table

我正在尝试将我的comment表与我的usercomment.userId=user.id在一起,例如comment.userId=user.id

不幸的是当我print_r($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};
    }

哪里

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

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

在我看来

$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>'
));

我看到一个小的错字,也许您在发帖时弄错了:

comment.userid=user.id

在这里它是userid并且您使用userId引用了它

请检查一下

编辑-问题后编辑

我对CommentableBehavior并不熟悉,但在我看来,您需要急于为每个Comment加载User模型:

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

我在getComments()方法中添加with('user')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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