How to pass a parameter to relational query?

I have a MANY_MANY relation:


'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)',  

             'condition'=>'some condiotions AND field_name=:param')

I get the result -instances of Myclass in the siteController:


$obj->rel

Is it possible (and how) to pass the :param from the controller to the relation’s query?

you can call relation as function instead of attribute (<- Yii magic here) and pass criteria:




$related = $obj->rel( array( 'condition'=>'some condtion', 'params'=>array( ':param1'=>xxx ), 'order'=>'order here' ) );



This will overwrite the conditions in the relation itself. I mean I’ll always have to write the non changing conditions defined in the relation.

it will not override anything. This condition will be AND-ed with those specified in relation.

Right. My mistake.

how to call it from gridview?

Thanks




//In griview columns

array(

   'header'=>'Date',

   'type'=>'raw',

   'name'=>'some_field',

   'value'=>'$data->methodName()'

),

//In Model

public function methodName(){

  if(isset($this->relation)){

        $out=$this->relation( array( 'condition'=>"start_date = :date",

            'params'=>array( ':date'=>$this->date )) );

        echo $out->field_name;

   }

}



Is it possible to override the condition?

No. the condition in relation definition has higher priority (and it is logical). You can however define another relation to the same target model but with different relation name and without condition, so you can apply any condition on it, like:




function relations() {

  return array(

    'relation'=>array( self::HAS_MANY, 'TargetModel', 'link_column', 'condition'=>'aaaaaaaaaaaaaa' ),

    'relationWithoutCondition'=>array( self::HAS_MANY, 'TargetModel', 'link_column' ),

  );

}



it will work just fine.