I am not sure if I’m doing this right or if I should change my schema to make this work more effectively.
I have three tables:
table: plant_attribute
id
maker_id
table: maintenance_event
id
plant_id --fk plant_attribute.id
table: service_schedule
id
maintenance_event_id – fk maintenance_event.id
and three models
Model: PlantAttribute
public function relations()
{
'maintenanceEvent' => array(self::HAS_MANY, 'MaintenanceEvent', 'plant_id'),
}
Model: MaintenanceEvent
public function relations()
{
'plantAttribute' => array(self::BELONGS_TO, 'PlantAttribute', 'plant_id'),
'serviceSchedule' => array(self::HAS_MANY, 'ServiceSchedule', 'maintenance_event_id'),
}
Model: ServiceSchedule
public function relations()
{
'maintenanceEvent' => array(self::BELONGS_TO, 'MaintenanceEvent', 'maintenance_event_id'),
}
I’m trying to retrieve maker_id from Model PlantAttribute in view ServiceSchedules/admin using CGridView
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'service-schedule-grid',
'dataProvider'=>$model->search(),
...
'id',
// i can get the plant_id from maintenanceEvents using this
'maintenanceEvent.plant_id'
// but how do i get maker_id from plantAttribute here?
...
)
Found a reason to verify what I wrote. All my examples should work. Note: I added an array to the with example. But you should be able to use lazy loading, so no need to specify with at this time. Just use one of the two suggested CGridView syntaxes. Hint: keep it simple, don’t add a lot of features (such as search and sort) from start.