Cgridview Filtering From A Table Not Directly Related

Hi!

Here’s my problem, I have 3 tables like this:

  • Brand

  • Realisation with a brand_id

  • Project with a realisation_id

I succeeded to filter in a project by the realisation name, because there are related, like this in the search function:


if($this->rea_name)

		{

			$criteria->together  =  true;

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

			$criteria->compare('realisation.name',$this->rea_name,true);

		}

$sort=new CSort;

		$sort->attributes=array(

				'id',

				'rea_name' => array(

						'asc' => 'realisation.name',

						'desc' => 'realisation.name DESC',

						'label' => 'Réalisation',

				),

				'*');

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort' => $sort,

		));



Thank to this topic http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

But now, I want to filter by brand and I tried to do the same but there are not related directly, I can’t do $criteria->with = array(‘brand’). Project table doesn’t know the brand table. I think I have to work with the realisation table to get the brand table but I don’t know how to do this!

So if anyone knows a solution, thank you! :)

You’ll need to include the bridging relation:


$criteria->with = array('realisation.brand');

That will include both the realisation relation and its brand relation.

Wow, thanks for the fast reply, it works!

Another small problem with my custom filtering.

I have an ‘ambigus column name’ when I’m trying to filter with two columns representing the name of project and the name of

a realisation per exemple. Do you know where I can put something like ‘realisation.name as realisation_name’ to avoid this error?

Can you post your filtering code?

Yes, here:




public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


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

		$criteria->compare('name',$this->name,true);

		

		if($this->rea_name)

		{

			$criteria->together  =  true;

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

			$criteria->compare('realisation.name',$this->rea_name,true);

		}

		

		if($this->brand_name)

		{

			$criteria->together  =  true;

			$criteria->with = array('realisation.brand');

			$criteria->compare('brand.name', $this->brand_name,true);

		}

		

		$sort=new CSort;

		$sort->attributes=array(

				'id',

				'rea_name' => array(

						'asc' => 'realisation.name',

						'desc' => 'realisation.name DESC',

						'label' => 'Réalisation',

				),

				'brand_name' => array(

						'asc' => 'brand.name',

						'desc' => 'brand.name DESC',

						'label' => 'Marque',

				),

				'*');

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort' => $sort,

		));

	}



It works with both realisation name and brand name together but when I try with the name of a project with one of them, I have the ambigus clause name.

Try using


$criteria->compare('t.name',$this->name,true);

I believe the main table is aliased as ‘t’.

Yes, it is.

Thanks again for the fast reply! It works perfectly ;)