G
G
Gorini42012-02-06 23:07:29
Yii
Gorini4, 2012-02-06 23:07:29

Search in Yii by argument with MANY_MANY relation

In the model (Part) there is a connection with another model (CarModel) of the form
'models'=>array(self::MANY_MANY, 'CarModel', 'part_to_model(p_id, m_id)')
what method to use for CDbCriteria in this case?
or ride $criteria->compare('models.name', $this->model)?
In the last construction, it is annoying that models will return not one object, but an array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
enchikiben, 2012-02-07
@gorini4

I would do a search like this: And I brought it to CGridView, and an additional method in the model, which will collapse the array of model values ​​in a row: I did something similar in a bunch of movies + genres.
$criteria->together=true;
$criteria->with=array('CarModel');
$criteria->compare('m_id',$this->m_id);

<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'name' => 'CarModel',
'filter'=> false,
'value' => ' $data->getCarModel ',
),
),
)); ?>

public function getCarModel($sSep = ', ') {
$aRes = array();
foreach ($this->CarModel as $itm) {
$aRes[] = $itm->name;
}
return implode($sSep, $aRes);
}

G
Gibbzy, 2012-02-07
@gibbzy

Method for what? What do you want to do? The question is not very clear.
There, if you want to pull parts for a specific car, then I usually use join.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question