Yii Framework Forum: need help displaying a relations field through CGridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

need help displaying a relations field through CGridView Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 22 January 2010 - 08:03 AM

hi all,

I have two tables.
Projects -
id
name

Gallery -
id
project_id //integer = Projects.id...foreign key in other words
photo
thumb

In my admin view of the gallery I would like instead of the project_id [which is an int] to display its respective name from table Projects. In my admin view I have:

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider'=>$dataProvider,
	'columns'=>array(
		'id',
                array(
                    'name'=>'name',
                    'value'=>'',
                ),
		'photo',
		'thumb',
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>


The name should be represented by the array column but I don't know what to put against value. Can anyone help me. I know that when I call the admin view the relations query is being executed [i can see that in the logs] and it is correct so I guessing its a matter of calling the value in the correct way.

My controller has the following admin action:
public function actionAdmin()
	{
		$dataProvider=new CActiveDataProvider('Gallery', array(
                        'criteria'=>array(
                            'with'=>array('project'),
                        ),
			'pagination'=>array(
				'pageSize'=>self::PAGE_SIZE,
			),
		));


		$this->render('admin',array(
			'dataProvider'=>$dataProvider,
		));
	}


project is the relation declared as:

return array(
                    'project'=>array(self::BELONGS_TO,'Projects', 'project_id'),
		);


Thanks for any help

cheers,
b
0

#2 User is offline   betelgeuse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 12-January 09

Posted 22 January 2010 - 09:32 AM

'name'='project.name'
a code snippet says more than a thousand words
0

#3 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 22 January 2010 - 10:27 AM

View PostFlavio, on 22 January 2010 - 09:32 AM, said:

'name'='project.name'


Thanks Flavio it works now. What is the logic behind this. I could not see this part explained anywhere.
0

#4 User is offline   betelgeuse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 12-January 09

Posted 22 January 2010 - 10:33 AM

I learned this from CGridView.php source:

 * $this->widget('zii.widgets.grid.CGridView', array(
 *     'dataProvider'=>$dataProvider,
 *     'columns'=>array(
 *         'title',          // display the 'title' attribute
 *         'category.name',  // display the 'name' attribute of the 'category' relation
 *         'content:html',   // display the 'content' attribute as purified HTML
 *         array(            // display 'create_time' using an expression
 *             'name'=>'create_time',
 *             'value'=>'date("M j, Y", $data->create_time)',
 *         ),
 *         array(            // display 'author.username' using an expression
 *             'name'=>'authorName',
 *             'value'=>'$data->author->username',
 *         ),
 *         array(            // display a column with "view", "update" and "delete" buttons
 *             'class'=>'CButtonColumn',
 *         ),
 *     ),
 * ));

a code snippet says more than a thousand words
0

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 22 January 2010 - 11:08 AM

View PostFlavio, on 22 January 2010 - 10:33 AM, said:

I learned this from CGridView.php source:

 * $this->widget('zii.widgets.grid.CGridView', array(
 *     'dataProvider'=>$dataProvider,
 *     'columns'=>array(
 *         'title',          // display the 'title' attribute
 *         'category.name',  // display the 'name' attribute of the 'category' relation
 *         'content:html',   // display the 'content' attribute as purified HTML
 *         array(            // display 'create_time' using an expression
 *             'name'=>'create_time',
 *             'value'=>'date("M j, Y", $data->create_time)',
 *         ),
 *         array(            // display 'author.username' using an expression
 *             'name'=>'authorName',
 *             'value'=>'$data->author->username',
 *         ),
 *         array(            // display a column with "view", "update" and "delete" buttons
 *             'class'=>'CButtonColumn',
 *         ),
 *     ),
 * ));




Ok hands up. I looked at this one millions times...i guess i need some sleep. Thanks again for your help
0

#6 User is offline   tgmbr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 27-April 10

Posted 27 April 2010 - 05:03 PM

View PostFlavio, on 22 January 2010 - 09:32 AM, said:

'name'='project.name'

I'm newbie here, so forgive me if question is silly... I did as it is written above however I cannot sort or search by this column (field from foreign table). How can I do this?
0

#7 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 09 May 2010 - 07:57 AM

View Posttgmbr, on 27 April 2010 - 05:03 PM, said:

I'm newbie here, so forgive me if question is silly... I did as it is written above however I cannot sort or search by this column (field from foreign table). How can I do this?


I have the exact same problem. Any ideas?
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 May 2010 - 01:54 AM

I've solved it like this:

    // CGridView column definition:
    'columns'=>array(
        array(
            'name'=>'Customer',
            'value'=>'$data->project->customer->shortname',
            'type'=>'text',
        ),


// Preparing the DataProvider in the model:
return new CActiveDataProvider(get_class($this),array(
    'sort'=>array(
        'multiSort'=>false,
        'attributes'=>array(
            'Customer'=>array(
                'asc'=>'customer.shortname',
                'desc'=>'customer.shortname desc',
            ),

1

#9 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 10 May 2010 - 10:43 AM

I've made it work like this. HTH
0

#10 User is offline   newscloud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 02-April 13

Posted 20 January 2014 - 01:58 PM

I've posted examples of using CGridView with relations as well as checkboxes here:
jeffreifman.com/yii/cgridview/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users