相关模型Yii的确定存在 [英] Yii determining existence of related models

查看:135
本文介绍了相关模型Yii的确定存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行的findAll查询只能返回在相关记录不存在的记录。

I want to run a findAll query that that only returns records where a related record doesn't exist.

谁能告诉我这是在Yii中做了什么?

Can anyone tell me how this is done in Yii?

上下文的情况下,只是有一点点可以帮助 -

Just a little bit of context in case it helps-

我工作的一个问卷调查应用程序,我的工作的对象是 -

I'm working on a survey application, the objects I'm working with are-

QuestionSurvey AnsweredQuestion

QuestionSurvey AnsweredQuestion

SurveyQuestion HAS_MANY AnsweredQuestion

SurveyQuestion HAS_MANY AnsweredQuestion

因此​​,我想回到QuestionSurvey车型在没有相关AnsweredQuestion存在。

I therefore want to return QuestionSurvey models where no related AnsweredQuestion exists.

在此先感谢,

尼克

推荐答案

如果您 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();

这篇关于相关模型Yii的确定存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆