CDbCriteria conditions

Hi all,

When retrieving records and using CDbCriteria conditions and wanting to use multiple conditions I find myself writing things like this :






$criteria=new CDbCriteria;


		$criteria->condition = '';


		


		if(isset($_GET['site_code']) && 


			Site::model()->exists('code = :site_code', array(':site_code' => $_GET['site_code']))) {


				


			$criteria->condition = "site_code = :site_code";


			$criteria->params = array(':site_code' => $_GET['site_code']);


			


		}


		


		if(isset($_GET['text_type']) && 


			TextType::model()->exists('name = :text_type', array(':text_type' => $_GET['text_type']))) {


			


			$criteria->condition .= (!empty($criteria->condition))? ' AND ' : '';


			$criteria->condition .= "TextType_id = :TextType_id";


			$criteria->params = array_merge($criteria->params, array(':TextType_id' => $_GET['text_type']));


			


		}


What I think is wrong is having to write the part where I check I already have set a condition string to insert the 'AND' if I have.

$criteria->condition .= (!empty($criteria->condition))? ' AND ' : '';

I think I am missing something …

How can I set multiple conditions without resorting to this somewhat unelegant code ?

thanks for any help on this

You may use:



$conditions=array();


$conditions[]=...;


$conditions[]=...;


$conditions[]=...;


$criteria->conditions=implode(' AND ',$conditions);


thanks qiang

small typo:

$conditions=array();

$conditions[]=…;

$conditions[]=…;

$criteria->condition = implode(' AND ', $conditions);

Quote

You may use:


$conditions=array();


$conditions[]=...;


$conditions[]=...;


$conditions[]=...;


$criteria->conditions=implode(' AND ',$conditions);