Yii correct syntax for conditions in relation definitions

余生颓废 提交于 2020-01-03 04:32:12

问题


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:


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?



来源:https://stackoverflow.com/questions/13211257/yii-correct-syntax-for-conditions-in-relation-definitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!