Problem Many_Many On Search()

Hi everyone,

First of all I’ve got to say that all my raltions in my models even MANY_MANY are working properly,so If I try to use $modelA->modelB… it works.

But I’m having problems using relations MANY_MANY in my search method with these models, e.g:

ModelA(restaurantes):




public function relations()

{

		

  return array(

	...

    'servicios' => array(self::MANY_MANY, 'Servicios', 'serviciosrestaurantes(servicios_id,restaurantes_id)'),...

		);

	}



ModelB(servicios):




public function relations()

	{

	return array(

		'serviciosRestaurantes' => array(self::HAS_MANY, 'ServiciosRestaurantes', 'servicios_id'),

		);

	}



And here a widget triying to use search method with these relationships:




$this->widget('bootstrap.widgets.TbThumbnails', array(

	'id'=>'restaurantes-grid',

	'dataProvider'=>$modelRst->search(array('condition'=>'servicios.id=1')),

	'template'=>"{items}\n{pager}",

	'itemView'=>'_rowRestaurante'

	)

);



When I try this, I get the following error:


CDbCommand ... SQL: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'servicios.id' in 'where clause'.

I’ve been checking the sql, and there aren’t inner joins of the MANY_MANY relationships, so obiously the column ‘servicios’ doesn’t exist.

And if I use


 var_dump($modelA->modelB);

I get array(0) { }…

Help :(

PD:I’ve just realized that in :


	public function search($param = array())

	{... 



I don’t have a




$criteria->with[]='model';

		$criteria->addSearchCondition('',$this->),true);

for this MANY_MANY relationship…Could it be the problem?Is there an special addSearchCondition for this MANY_MANY relationships???

Start here.

In your search method, you’re going to need to make sure that the CDbCriteria is run with the relevant relation and that you’ve specified the “together” option. There are pitfalls though, so go through the above link first.

Use case 2 is probably the one that applies to you.

I’ve found the solution in the controler I’ve generated a new dataProvider instead of use the search method:


  $dataProvider=new CActiveDataProvider('ModelA', array(

	     'criteria'=>array(

	        'with'=>array('tableModelB'=>array( 'condition'=>'', 'together'=>true),

	        	

	        	),

	        'condition'=>'',

	     ),

	     'pagination'=>array(

	         'pageSize'=>10,

	     ),

	     ));

and I’ve used this dataProvider for my gridview and it works!

Thanks ;)

Okay. You should be aware that your pagination won’t work correctly unless you follow the link that I gave you though.