Yii CDbCriteria Join

How can I write a request

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

with CDbCriteria syntax?

+6
sql php yii criteria
source share
1 answer

In fact, you cannot completely write this, since you need to apply the criteria to the activerecord model to get the primary table, but provided that you have the DocDocs model, you can do it this way:

 $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 this can be much simpler if you give your DocDocs model a relationship with doc_access, then you do not need to use 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 good idea how to get started ...

+13
source share

All Articles