how i can use CDbCriteria

Hi everyone, I need some help from you guys, I need to implement this query with CDbCriteria feature of Yii framework, I hope someone can help me.




Select * FROM pets WHERE type = 1 AND category = 1 AND (color = 'black' OR color = 'blue' OR color='red')



Saludos desde Mexico Thanks!


$model = new <your model class>;

		

$criteria = new CDbCriteria;

$criteria->condition="type=:type AND category=:category AND (color=:color1 OR color=:color2 OR color=:color3)";	 	

$criteria->params=array(':type'=>1, ':category'=>1, ':color1'=>'black', ':color2'=>'blue', ':color3'=>'red');

	 	

$results = $model->findAll($criteria);



Try this with IN condition:




$criteria = new CDbCriteria();

$criteria->addColumnCondition(array('type' => 1, 'category' => 1));

$criteria->addInCondition('color', array('black', 'blue', 'red'));

$model = TestModel::model()->find($criteria);



or write rough sql query:




$criteria = new CDbCriteria();

$criteria->condition = "type = 1 AND category = 1 AND (color = 'black' OR color = 'blue' OR color = 'red')"; // you may supply parameters here



More information is here CDbCriteria

Please try this, I didn’t test it.

Cheers

@ Aruna Attanayake

Hahahah we posted almost the same answer at the same time! :)