Hi all,
I’m trying to do something that appears to be common and straight forward, but all the other questions around this issue either deal with some sort of special case. I spent a couple of hours reading and testing to no avail. So here’s the problem…
I have two business objects: a Thing, and a Status. A Thing belongs to a Status and a Status has many Things.
This is modelled in Yii with two models, and the relationship is defined in the Thing model:
'currentstatus' => array(self::BELONGS_TO, 'Status', 'status_id'),
Some of the Status have a property which mean they are ‘active’ and some ‘inactive’. To tell if a Thing is ‘active’ we have to look at the related status to see which type it is.
The use case I’m trying to solve is that we want to look at a grid view of Things and select only ‘active’ things.
In the controller I have the action:
public function actionReportActive()
{
$model=new Thing('search');
$model->unsetAttributes(); // clear any default values
// ## Some criteria here ? ?
$this->render('reportactive',array(
'model'=>$model,
));
}
and then the $model is rendered in the ‘reportactive’ view using a TBgridView.
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider'=> $model->search(),
// ## some criteria here ? ?
'filter'=>$model,
'columns'=>array(
'name',
'size',
'currentstatus.name',
'currentstatus.active',
array(
'class'=>'CButtonColumn',
),
),
));
As you can see from the two snippets, I have experimented with putting criteria in both the search definition in the controller and the grid view. I can make this work either way with simple searches based on the Thing properties, but I cannot work out how to say "get only the Things that have a currentstatus.active = 1.
I feel there must be an easy solution to this, as its a very common case. Can someone show me how to do it?
thanks