问题
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