Help With Many-To-Many Relation

Thanks in advance.

I have a many-to-many relation with models:


'localizacoes' => array(self::MANY_MANY, 'Localizacoes', 'dispositivos_local(id_dispositivos, id_local)'),


 'dispositivos' => array(self::MANY_MANY, 'Dispositivos', 'dispositivos_local(id_local, id_dispositivos)'),

how can i filter cgridview of dispositivos by field of localizacoes.id_projeto

public function search()



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

		// should not be searched.

		$criteria=new CDbCriteria;


		$criteria->compare('localizacoes.id_projeto',  Yii::app()->user->projeto,true);

		return new CActiveDataProvider($this, array(




Column not found: 1054 Unknown column ‘localizacoes.id_projeto’ in ‘where clause’


$criteria->together = true;

yep,thanks a lot

Also, be very aware of the problems with pagination in HAS_MANY and MANY_MANY relations. This page is very helpful.

And in cgridview



        'value'=>'$data->localizacoes->local . " ," . $data->localizacoes->distrito',



i have

Trying to get property of non-object

If it’s a MANY_MANY relationship, $data->localizacoes will be an array. You would need to either iterate through all of the elements or use a specific one by index:


        ? $data->localizacoes[0]->local . " ," . $data->localizacoes[0]->distrito

        : ""',

Or a less ugly construct if you prefer…


and how to do this

How i can filter if isset($_GET(‘idlocalizacao’))?

$criteria->compare('localizacoes.id_local',  isset($_GET('idlocalizacao'))? $_GET('idlocalizacao'):NULL);

thanks again.

You shouldn’t be accessing $_GET in your model, that’s the job of the controller.

Read this page for information on the appropriate way to handle this.

But,Keith ,then i get again

Trying to get property of non-object

Read through the link that I gave you; you need to implement each part of that to get this to work correctly.

but what about the many-many relation.

Maybe need a coffee

The technique works fine with MANY_MANY, but you need to also take the pagination issues into account.

First, get the filter working with the help of this page.

Then, fix pagination issues with the help of this page.

The second link might also help you to understand how the filters work with MANY_MANY relations.

EDIT: Also, coffee is recommended ;)