Posted By

drock on 06/19/12


Tagged

Yii


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

drock


Yii: Using MySQL's IN condition with Active Record


 / Published in: PHP
 

When working with your model's search function or just grabbing some data with findAll, you can't add an IN condition to your condition clause. Fail: $criteria->condition = 'start=:startID AND end=:endID AND status IN (1,2,3)';

Instead, use addInCondition:

  1. $criteria = new CDbCriteria();
  2. $criteria->condition = 'start=:startID AND end=:endID';
  3. $criteria->params = array(':startID'=>$activeProfileID,':endID'=>$pageID);
  4. $criteria->addInCondition('status',array(6,7));
  5. $results = GraphEdge::model()->findAll($criteria);
  6.  
  7. //OR
  8.  
  9. $theEdge = new GraphEdge();
  10. $results = $theEdge->findAllByAttributes(array('start'=>$activeProfileID,'end'=>$pageID,'status'=>array(6,7)));

Report this snippet  

You need to login to post a comment.