Yii correct syntax for conditions in relation defi

2019-06-09 14:59发布

I'm struggling to get the correct syntax for a relation condition I'm trying to set. The main relation is set with the foreign key question_id, but also contained in the child table is the user_id column.

I wish to return only records related to the logged in user. Here's what I've got so far-

  class SurveyQuestion extends CActiveRecord {
 .......

   public function relations()
    {
        return array(           
            'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id',
                'condition'=>"answered_questions.user_id = Yii::app()->user->id'"),
        );
   }

Please can someone correct my syntax which is so far not working? I've not been working with Yii for very long and so I wouldn't be surprised if my 'condition' clause was all wrong.

Many thanks,

Nick

1条回答
放荡不羁爱自由
2楼-- · 2019-06-09 15:29

answered_questions.user_id = Yii::app()->user->id' will check if answered_questions.user_id is equal to the string 'Yii:: .....' Not the actual user ID. You need to end the quote and append it as a PHP command:

'condition'=>"answered_questions.user_id = ".Yii::app()->user->id),

Unless this was a typo only in your question?

查看更多
登录 后发表回答