E
E
enchikiben2012-01-21 18:19:05
Yii
enchikiben, 2012-01-21 18:19:05

Yii search in many_to_many?

Good night, tell me Yii experts.
There is a directory of disks (Drivers), each disk has genres (genres) (a disk can belong to several genres).
Connected them via many_to_many
public function relations()
{
return array(
'genres' => array(self::MANY_MANY, 'Genres', 'drives_in_genres(driver_id,genre_id)'),
);
}
I can't understand how the search for multiple records works, it either groups them or I don't know ... tell me.
here is the search method
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',
$criteria->compare('user',$this->user);
$criteria->compare('name',$this->name,true);
if ( is_numeric($this->year_from) ) {
$criteria2 = new CDbCriteria;
$criteria2->condition = 'year '.( !isset($this->year_to) || intval($this->year_to) <=0? "=": ">=" ).' :year_from';
$criteria2->params = array(':year_from' => intval($this->year_from));
$criteria->mergeWith($criteria2);
}
if ( is_numeric($this->year_to) ) {
$criteria2 = new CDbCriteria;
$criteria2->condition = 'year '.( !isset($this->year_from) || intval($this->year_from) <=0? "=": "<=" ).' :year_to';
$criteria2->params = array(':year_to' => intval($this->year_to));
$criteria->mergeWith($criteria2);
}
//// Here I hook the records and look for them
$criteria->compare('genre_id',$this->genresIds);
$criteria->with=array('drivesInGenres');
// when this option is disabled, the output is normal, but the search does not work
$criteria->together=true;
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array(
'pageSize' => 20,
),
));
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2012-01-21
Protko @Fesor

$criteria->together=true; allows you to skip the table name when specifying fields. That's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question