Yii: Using MySQL\'s IN condition with Active Record


/ Published in: PHP
Save to your folder(s)

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:
<code>$criteria->condition = 'start=:startID AND end=:endID AND status IN (1,2,3)';</code>

Instead, use addInCondition:


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.