How can i add relational data in a gridview including the search. So i have a contact which can have one to many locations. One of the locations is used as the main-adress. My plan is to add those fields to the standard gridview (manage) which was generated by the CRUD-generator.
model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'locations' => array(self::HAS_MANY, 'Locations', 'contact_id'),
'persons' => array(self::HAS_MANY, 'Persons', 'contact_id'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
I couldn’t find any solution either. Instead I modified CGridView to recognize a ‘|’ character in the first spot of defining a field in CGridView. Usually you provide Name[:Type][:Label] to define a field. I changed it to Name[|Name][:Type][:Label] and then I changed the code to understand the | and to pull the relationship after that pipe and print a comma separated list to the data cell.
So for example, you could just write Parent|Children where parent is a defined member of the parent model and children is a relationship rule in the parent parent model. In each data cell in the Parent column of the GridView it would output Child1, Child 2, Child 3, etc…
It worked for what I needed but then I couldn’t figure out how to extend CGridView to implement this correctly.