Yii Framework Forum: Problem Many_Many On Search() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem Many_Many On Search() Problem MANY_MANY on search() Rate Topic: -----

#1 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 29 May 2013 - 05:31 AM

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???
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,655
  • Joined: 04-March 10
  • Location:UK

Posted 29 May 2013 - 05:34 AM

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.
0

#3 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 29 May 2013 - 08:49 AM

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 ;)
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,655
  • Joined: 04-March 10
  • Location:UK

Posted 29 May 2013 - 08:56 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users