lvil
(Leonid Vil)
February 22, 2012, 2:13pm
1
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?
redguy
(Maciej Lizewski)
February 22, 2012, 2:47pm
2
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' ) );
lvil
(Leonid Vil)
February 22, 2012, 3:09pm
3
redguy:
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.
redguy
(Maciej Lizewski)
February 22, 2012, 3:43pm
4
it will not override anything. This condition will be AND-ed with those specified in relation.
wiwitiwan
(Wiwitiwan)
April 26, 2012, 2:38am
6
how to call it from gridview?
22francis
(Jesusloves Francis)
December 3, 2013, 1:30pm
8
//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;
}
}
GSTAR
(Omzy83)
August 20, 2014, 3:21pm
9
Is it possible to override the condition?
redguy
(Maciej Lizewski)
August 21, 2014, 1:16pm
10
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.