How make Yii condition on Many_Many relations

4.4k Views Asked by At

have tables:

    place (id, title, state);
movie (id, title, state);
schedule (place_id, movie_id, time);

use simple yii models:

    place.relations:
'movies' => array( self::MANY_MANY, 'movie', 
               'schedule(place_id,movie_id)', 
               'condition' => 'time > now()' ),

use simple yii controller:

    $tmp = new Place();
$res = $tmp->findAll();
var_dump( $res[0]->movies );

yii returns a full list with the out state status :(

How can I get movies with the condition movie.state = 1 ?

2

There are 2 best solutions below

0
On

If I'm reading your relations correctly, this should work.

$res = Place::model()->with('movies')->findAll(array('condition'=>'movies.state =1'));

In any case, consult the documentation for more advanced query options :) http://www.yiiframework.com/doc/api/1.1/CActiveRecord#find-detail

0
On

If it's a conditional state:

$state = $_POST['state'];
$res = Place::model()->with('movies')->findAll(array('condition'=>'movies.state=:m_state', 'params'=>array(':m_state'=>$state)));

Depending on version of Yii, you can remove the ':' in the array keys of params. Read up about them and mark the previous answer as best already.