Yii CDbCriteria 加入 [英] Yii CDbCriteria Join

查看:27
本文介绍了Yii CDbCriteria 加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写查询

SELECT *
  FROM doc_docs dd 
  JOIN doc_access da 
    ON dd.id=da.doc_id 
   AND da.user_id=7

使用CDbCriteria 语法?

推荐答案

你实际上不能完全写出来,因为你必须将标准应用到 activerecord 模型来获取主表,但假设你有一个 DocDocs 模型你可以做像这样:

You actually cant completely write that since you have to apply the criteria to an activerecord model to obtain the primary table, but assuming you have a DocDocs model you can do it like this:

$oDBC = new CDbCriteria();
$oDBC->join = 'LEFT JOIN doc_access a ON t.id = a.doc_id and a.user_id = 7'; 

$aRecords = DocDocs::model()->findAll($oDBC);

虽然如果您为 DocDocs 模型提供与 doc_access 的关系可能会容易得多,但您不必使用 dbcriteria:

Although it might be a lot easier if you give your DocDocs model a relation with doc_access, then you don't have to use the dbcriteria:

class DocDocs extends CActiveRecord
{
   ... 

   public function relations()
   {
      return array('access' => array(self::HAS_MANY, 'DocAccess', 'doc_id');
   }

   ...
}

$oDocDocs = new DocDocs;
$oDocDocs->id = 7;
$aRecords = $oDocDocs->access;

应该给你一个很好的想法如何开始......

Should give you a fairly good idea how to start...

这篇关于Yii CDbCriteria 加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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