且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相关模型Yii的确定存在

更新时间:2023-11-22 10:00:22

如果您 SurveyQuestion ::模型() - >在('AnsweredQuestion') - >的findAll()这将自动发生。因为它会拉都下降了一个连接在一起的记录 INNER JOIN (除非你告诉它,否则),因此它不会拉有任何疑问下来,如果他们没有答案。

If you SurveyQuestion::model()->with('AnsweredQuestion')->findAll() this will happen automatically. Because it will pull all the records down together joined by an INNER JOIN (unless you tell it otherwise) and it will therefore not pull any questions down if they have no answers.

......我想。

更新
从您的意见好了,我把它缠在我的头上。实际上,你要查看所有 SurveyQuestions ,其中 AnsweredQuestions 不为它存在。在这种情况下,你想Yii的执行 LEFT JOIN 这将拉低了连接表空记录,如果某行不存在。然后,你需要一个条件添加到其中列明了关系,其中 AnsweredQuestion.id为NULL (或任何你的主键是,其实可以是任何领域,但主键是个好习惯)。

Update
OK from your comments, I got it wrapped around my head. You actually want to view all the SurveyQuestions where AnsweredQuestions don't exist for it. In which case you want Yii to perform a LEFT JOIN which will pull down a NULL record for the joined table if a row doesn't exists. Then you need to add a condition to the relationship which states where AnsweredQuestion.id is NULL (Or whatever your primary key is, actually can be any field but primary key is good practice).

如果这是一个单实例类的话,而不是一个更长久的关系,那么你可以这样做:

If this is a single instance kind of thing as opposed to a more permanent relationship then you can do:

SurveyQuestion::model()->with(array(
    'AnsweredQuestion'=>array(
        'joinType'=>'LEFT JOIN', 
        'condition'=>'`AnsweredQuestion`.`id` is NULL')
    )->findAll();