Yii query returning no results

2019-06-01 19:09发布

问题:

I'm struggling to create the correct query in Yii, I believe I'm making progress though.

I need to check a related table and only want to return records that don't have a record in the related table. This was answered here- Yii determining existence of related models

What is complicating this and I'm unsure how to overcome it, is that multiple users can have records in this related table. Therefore the full requirement is to return records where no related record exists, but only counting records for the logged in users.

The two related objects are as follows- SurveyQuestion AnsweredQuestion

SurveyQuestion HAS_MANY AnsweredQuestion

AnsweredQuestion table has the following columns-

id - survey_question_id - user_id

survey_question_id is the foreign key for the SurveyQuestion table.

My approach so far is to try and limit the records to those relevant to the logged in user with the relation definition-

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'survey_answer'=>array(self::HAS_MANY,'SurveyAnswer','survey_question_id'),
        'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id',
            'condition'=>'answered_questions.user_id = '.Yii::app()->user->id,
            'joinType'=>'LEFT JOIN',
            ),
    );
}

To limit the the query to records in the parent table with no relevant ones in the child table I've used a condition in the findAll function like so-

            $questions = SurveyQuestion::model()->with(array(
                                            'survey_answer',
                                            'answered_questions'=>array(

                                                    'select'=>false,

                                                    'joinType'=>'LEFT JOIN',
                                                    'condition'=>'`answered_questions` . `id` is NULL'
                                                    ),))->findAll();

The two pieces of code return no results even when the child table is cleared.

Can anyone spot where I'm going wrong, in approach or execution?

Many thanks,

Nick

Update

As requested, here's the sql statement that is run. It's the second Join that is relevant, the first Join collects the multiple choice answers.

SELECT `t`.`id` AS `t0_c0`, `t`.`area_id` AS `t0_c1`,
`t`.`question_text` AS `t0_c2`, `t`.`date_question` AS `t0_c3`,
`survey_answer`.`id` AS `t1_c0`, `survey_answer`.`survey_question_id` AS
`t1_c1`, `survey_answer`.`answer_text` AS `t1_c2`, `survey_answer`.`tag_id`
AS `t1_c3` FROM `tbl_survey_questions` `t`  LEFT OUTER JOIN
`tbl_survey_answers` `survey_answer` ON
(`survey_answer`.`survey_question_id`=`t`.`id`)  LEFT JOIN
`tbl_answered_questions` `answered_questions` ON
(`answered_questions`.`question_id`=`t`.`id`)  WHERE
((answered_questions.user_id = 2) AND (`answered_questions` . `id` is
NULL))

回答1:

Had a though after I posted the comment about visually running your query.

I think you need to put the condition for user_id in the on clause of the relation, rather than the condition clause. Because it's only returning parent rows where the child has NULL id and user_id of 2. This will obviously never happen. But you need it to be in the JOIN criteria. So it should read:

'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id',
    'on'=>'answered_questions.user_id = '.Yii::app()->user->id,
    'joinType'=>'LEFT JOIN',
),