Column CGridView

Can I add a column of a different model in a CGridView another model??

Can you help me?????

Yes, why not? The simplest way to connect the two models with a relation entry and you can use it freely. For instance:

$this->widget(‘zii.widgets.grid.CGridView’, array(

 'dataProvider' => $dp,


 'id' => 'my_grid',


 'columns'=>array(


  array( 


          'name'=>'Date',


          'value'=>'date("M j, Y  H:i", $data->start_time)',


      ),


      array(


      'name'  => 'User',


          'value' => '$data->profile->full_name',


      ),

… etc…

One model contains start_time, and another contains full_name member. So you can fill the ‘User’ column with a data from another model accessing profile relation from the 1st model.

Do you ask this?

regards:

Balázs

Excuse me but the code that you have write is not so clear…

I don’t understand these lines…




'name'=>'Date',

              'value'=>'date("M j, Y  H:i", $data->start_time)',

          ),

          array(

	      'name'  => 'User',

              'value' => '$data->profile->full_name',




‘Date’ and ‘User’–>what are??Name of column?Of model???

$data->profile->full_name or $data->start_time—>Also these…what are??

Yes this is the part of a view file this ‘configures’ the CGridView. Check out the yii docs about the possible properties.

‘name’: key that column name (header title)

In my example Date and User.

‘value’: key tells how generate data in each row

So im my example:

$data->profile->full_name

and

$data->start_time

comes from 2 models.

The first model contains two members: start_time AND the profile relation entry! data members

The second model contains: full_name data member.

Hopefully now clear, good luck

Balázs

Yes i have tried but i have error…

Property "accesso.ruoli" is not defined.

My code is





<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'ACCESSO_ID',

		'USER_NAME',

		'PASSWORD',

		//'ANAGRA_ANAGRA_ID',

		'SSO_USER',

		'APPROVATO',

		array(  // display 'author.username' using an expression

            'name'=>'Ruolo',

            'value'=>'$data->ruoli->DESCRIZIONE',

        ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



My model name is ‘ruoli’ and the name of attribute i want to insert as a column is ‘DESCRIZIONE’

Can you help me???

Try this: ‘$data->DESCRIZIONE’ (without ‘$data->ruoli->DESCRIZIONE’)

if toes not work check out your $dataProvider definition code (where you create the CActiveDataProvider object) and the rule settings between your models.

Dear GBA.

dont we should decare the relations function first?

i have the same issue. and my script is like this :

my table is such this :

User = (id, user_name,role_number)

Role = (id,Role_name).

when the data User is listed, I want the role_name to be shown not the ID

the partial of the coding is down below, that i have configured.

user.php =>

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(

‘role_relation’ => array(self::BELONGS_TO, ‘Role’, ‘id’),

);

}

============================================

UserController.php =>

public function actionIndex()

{

$dataProvider=new CActiveDataProvider(‘User’);

$this->render(‘index’,array(

‘dataProvider’=>$dataProvider,

));

}

============================================

index.php =>

$this->widget(‘zii.widgets.grid.CGridView’, array(

‘dataProvider’=>$dataProvider,

‘columns’=>array(

‘id’,

‘user_name’,

‘password’,

array(‘name’=>‘role_number’, ‘header’=>‘Role Name --’,‘value’ =>’$data->role_relation->role_name’ ),

),

));

but i am getting this error message : Trying to get property of non-object

can u guys help me out?

thank you.

Hi teuku,

Did you let Gii generate the relation above? Or did you write it manually?

If the former, you might have an error when you created the foreign key constraint. Check your user table.

If the latter, the relation should be like this:




public function relations()

{

    return array(

        'role_relation' => array(self::BELONGS_TO, 'Role', 'role_number'),

    );

}



Maybe you should try rename db field role_number to role_id (if that’s your foreign key in User table) and set correctly foreign key in db.

This topic originally about CGridView not db issues,that’s why I did not write all the details.

Anyway did you tested Ur db model, is that working correctly, I suppose not.

Ok I see in the meantime softark answered your problem.

Hi Softark,

thank You for your response. yes i wrote them manually, because at the moment I don’t want to change de DB structur.

And thank You so much,you pointer hits the target so well…You solved my problem.

thanks dude.