Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 241
NewUser Avatar asked Oct 08 '22 21:10

NewUser


1 Answers

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'
        ),
    ));
?>

screen capture of instanced page

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...

like image 80
CapelliC Avatar answered Oct 13 '22 11:10

CapelliC