If you are new to Yii, you are probably like me and stunned with the awesomeness of CDbCriteria. I found some good article on the web on how to use CDbCriteria and love to share.
1. Yii Documentation is always the best place to cross check, too bad it does not have detail walk through or how-to http://www.yiiframework.com/doc/api/1.1/CDbCriteria#condition-detail
2. Kevin has a decent series of posts about Yii and Database that worth to have a look @ http://www.kevinkorb.com/post/26
3. Yii forum provides 1 good example of translate SQL to CDbCriteria
http://www.yiiframework.com/forum/index.php?/topic/9918-advanced-use-of-cdbcriteria/page__p__48806__hl__cdbcriteria#entry48806
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SELECT COUNT(keyword) AS relevance, i.id, d.title // column list excluded from join table FROM Index i JOIN Documents d ON (i.id=d.id) WHERE d.id IN ( SELECT id FROM Index WHERE keyword = 'word' ) GROUP BY id ORDER BY relevance DESC |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
< ?php $criteria=new CDbCriteria; $criteria->alias='i'; $criteria->select= 'COUNT(keyword) AS relevance, i.id, d.title'; $criteria->join= 'JOIN Documents d ON (i.id=d.id)'; $criteria->condition=> 'd.id IN ( SELECT id FROM Index WHERE keyword = "word" )'; $criteria->group='id'; $criteria->order=' relevance DESC'; ?> |