Reputation: 13323
I have the database just like this
==== Group =====
id
name
==== Member ====
id
group_id
firstname
lastname
Now I can use Member member's table attributes in group controller just by using multimodel. As I have done multimodel so I can easily make create update delete for both models from a single view page. Now my problem is when I am going to shoew both models in Group's admin view file I have to show both files in CGridView to show in a Grid. But my problem is in CGridView only first model can be seen.I want the second models to be shown on the CGridView. So how to do that?
Upvotes: 5
Views: 6462
Reputation: 60034
I think you need to combine models using the Relational Active Record. In the process of self learning I'm following, I hope I will shortly have an example to post here...
EDIT finally, I worked out an example. I've (maybe) found a bug in Yii, so what is an easy task, required more time than necessary...
I have two models, User e Persona, obtained from gii, previously unrelated. Then I add a Persona to User as optional attribute: in User.php
/**
* @return array relational rules.
*/
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(
'persona' => array(self::HAS_ONE,'Persona','id')
);
}
then the model for User automatically display selected fields from Persona, when bound to CGridView:
<hr><?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => new CActiveDataProvider('User'),
'columns' => array(
'username',
'password',
'email',
'persona.indirizzo'
),
));
?>
The bug I found (perhaps, need to investigate more): my Persona model has an attribute with an accented character in name, and if I use that instead I get an error: i.e. if
'columns' => array(
'username',
'password',
'email',
'persona.identità'
),
then the page can't be instanced:
The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.
/home/carlo/public/yii-1.1.8.r3324/framework/zii/widgets/grid/CGridView.php(332)
320 foreach($this->columns as $column)
321 $column->init();
322 }
323
324 /**
325 * Creates a {@link CDataColumn} based on a shortcut column specification string.
326 * @param string $text the column specification string
327 * @return CDataColumn the column instance
328 */
329 protected function createDataColumn($text)
330 {
331 if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$text,$matches))
332 throw new CException(Yii::t('zii','The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
333 $column=new CDataColumn($this);
334 $column->name=$matches[1];
I think it's the regular expression that mismatch...
Upvotes: 4
Reputation: 5798
Define some function like 'getGroupNameById' in group model then call it as follows
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider, //for members model
'columns'=>array(
'id',
'firstName',
'lastName',
array(
'name'=>'groupName'
'value'=>'getGroupNameById($data->group_id)',
//$data is members row accessible in gridview
),
),
));
Check this post for more details CGridView-Render-Customized-Complex-DataColumns
Upvotes: 0
Reputation: 8408
If you add getters for groupId and groupName to the Member model you can easily display the gridview for members and include their group data. It's not an extremely clean solution, but it's the easiest.
Upvotes: 0