First off, Yii is a great product. It’s really speeding my development along.
I have nested relations that I would like to sort/filter on in the CGridView.
The one level of relations works (InventoryStatus), but I can’t get the 2nd level of relation (Edition Name) to sort/filter. I also have the offending relation working in a different model (1 level of nested relation)
I think it has something to do with the dot notation in the With() edition.editionType and the related CSort array.
If I try to decouple the names, I get compile errors. Relation "whatever the attempt was" is not defined in active record class "Inventory".
Am I missing something basic here?
Inventory belongsTo Edition belongsTo EditionType
The FKs are related by ids
In Inventory Model (CDbCriteria)
$criteria->with = array('inventoryStatus','edition.editionType');
$criteria->addSearchCondition('inventoryStatus.name',$this->inventoryStatus);
$criteria->addSearchCondition('editionType.name',$this->edition->editionType);
In Inventory Model (CSort)
$sort = new CSort();
$sort->defaultOrder = 'editionType.name ASC';
$sort->attributes = array(
'id',
'edition.editionType' => array(
'asc'=>'edition.editionType.name',
'desc'=>'edition.editionType.name DESC',
),
'inventoryStatus' => array(
'asc'=>'inventoryStatus.name',
'desc'=>'inventoryStatus.name DESC',
),
);
In the view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'inventory-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'name'=>'inventoryStatus',
'value'=>'$data->inventoryStatus->name',
'filter'=>CHtml::listData(InventoryStatus::model()->findAll(array('order'=>'name')), 'name', 'name'),
),
array(
'name'=>'edition.editionType',
'value'=>'$data->edition->editionType->name',
'filter'=>CHtml::listData(editionType::model()->findAll(array('order'=>'name')), 'name', 'name'),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>