Show FK reference "Name" on manage Grid

Hi guys, i’am really new on Yii, and i think ist amazing!

I downt know how to do this…

This is my actual code…


<?php 


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

	'id'=>'usuarios-descargas-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'idRelacion',

		'usuario',

		'descarga',

		array(

			'class'=>'CButtonColumn',

		),

		

	),

	

	

)); ?>

"usuario" and "descarga" are FK, so i need to get his name from "descargas" and "clientes" model.

How can do it?

Thanks in advance!

Create "BELONGS TO" relation as explained here and than you will be able to get name in such way:




'columns' => array(

  ...

  'usuariorelation.name',

  ...

),



I understood you but im doing something wrong…

I got 3 Models:

  1. clientes(clients)

  2. descargas (Downloads)

  3. usuariosDescargas (User(from client)-downloads)

"Clients" structure: (Only important fields…)

  • idCliente (Int)

  • usuario (Varchar)

"descargas" structure:

  • idDescarga (int)

  • titulo (varchar, title)

"usuariosDescargas" structure:

  • idDescarga (Int)

  • usuario (Int, FK from idCliente)

  • descarga (Int, FK from idDescarga)

So in manage list (admin.php) from my Model "usuariosDescargas"

i wrote this:


<?php 


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

	'id'=>'usuarios-descargas-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'idRelacion',

		'usuario.usuario',

		'descarga',

		array(

			'class'=>'CButtonColumn',

		),

		

	),

	

	

)); ?>

Ok, i want Username (field usuario from clients model)

So i modified "clientes.php" in models, and Added relationship…


	/**

	 * @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(

			'idCliente' => array(self::HAS_MANY, 'usuariosDescargas', 'usuario')

		);

	}

Then modified usuariosDescargas.php and added relationships…


/**

	 * @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(

			'usuario' => array(self::BELONGS_TO, 'clientes', 'idCliente'),

			'descarga' => array(self::BELONGS_TO, 'descargas', 'idDescarga') 

		);

	}

But in manage list from usuariosDescargas i got empty field, from "usuario.usuario", i know i know i doing something wrong but, where??

Thanks!!

In this relation


'usuario' => array(self::BELONGS_TO, 'clientes', 'idCliente'),

you need to specify foreign key


'usuario' => array(self::BELONGS_TO, 'clientes', 'usuario'),

and use the name of relation which is not equal to the name of field which already exists in model.

Thank you, this work perfectly!!

Hi,

I have a same requirement and I followed the same steps in my code.

But the following error is printed,

     Property&quot;activity_role_map.activities&quot;  is not defined.

In the ‘zii.widgets.grid.CGridView’,

    'columns'=&gt;array(


          'id',


          //'activity_id',


          'activities.activity',


          ...


     );

The relations in the model file is defined as mentioned.