How to use filter for the relational table ??

There is a very simply function to handle the filtering for the model, like:


$criteria->compare('category_name',$this->category_name);

But if there is a relation, for example "products", which means one category has name products

How can i apply the compare function to the relational table??

I tried the following, but it’s very tedious to have a “if” checking before the “with” property




if($this->product_name){

    $criteria->with = array(

			'products'=>array('condition'=>'product_name =' . $this->product_name),

		);

}else{

    $criteria->with = array('products');

}




Thanks all. :-*

You can set the condition in the conditiond for the main model:




$criteria=>addcondition('product_name =' . $this->product_name);



But the product is not a main model, it’s a relation model actually.

So what can i do??

if you use with (products), Yii will select all the field of product, included product_name.

Are you getting some error doing like that?

I used this


$criteria->compare('products.product_name',$this->product_name);



But i got this error "Undefined table: 7 ERROR: missing FROM-clause entry for table "products""

It’s because I am using lazy loading for the products table (not join table)

[color="#FF0000"]Updated:[/color]

I tried to create another CDbCriteria object to help to generate the product condition query, the code as below:




$productsCriteria = new CDbCriteria;

$productsCriteria->compare('product_name', $this->products_product_name);


		$criteria->with = array(

			'products' => array('condition' => $productsCriteria->condition),

		);



But this time it’s very strange, the products’s condition merged with the main model(category)'s condition

(i’m using lazy loading, not joining the table, it’s incorrect if merged the product condition to main model

which also gave me the PDO error (the parameter was not blinded)

Can anyone help me??