php Yii CDbCriteria 加入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4983864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii CDbCriteria Join
提问by Marat_Galiev
How I can write the query
我如何编写查询
SELECT *
FROM doc_docs dd
JOIN doc_access da
ON dd.id=da.doc_id
AND da.user_id=7
with CDbCriteria
syntax?
用CDbCriteria
语法?
回答by Blizz
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:
您实际上无法完全编写,因为您必须将标准应用于 activerecord 模型才能获取主表,但是假设您有一个 DocDocs 模型,您可以这样做:
$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);
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:
虽然如果您为 DocDocs 模型提供与 doc_access 的关系可能会容易得多,但您不必使用 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...
应该给你一个很好的主意如何开始......