Using CGridView to show another model's attributes

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?

-------------Problems Reply------------

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:

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => new CActiveDataProvider('User'),
'columns' => array(

Using CGridView to show another model's attributes

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(

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.


320 foreach($this->columns as $column)
321 $column->init();
322 }
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...

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.

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
//$data is members row accessible in gridview

Check this post for more details CGridView-Render-Customized-Complex-DataColumns

Category:php Views:999 Time:2012-01-30

Related post

Copyright (C), All Rights Reserved.

processed in 0.316 (s). 11 q(s)