Yii Framework Forum: Searching A Gridview Comprised Of Related Tables - Yii Framework Forum

Jump to content

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

Searching A Gridview Comprised Of Related Tables Rate Topic: -----

#1 User is offline   John McL 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-October 12

Posted 28 October 2012 - 01:09 AM

In Larry Ullman's tutorial 'Learning the Yii Framework' he creates a small app with two tables Employee and Department.
On the 'Manage Employee' page there is a grid view where an admin user can filter Employee records. However there is only a 'Department Id' column, and so I added a 'Department Name' column for readability, as shown in the following code:
<?php 
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'employee-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'id',
		'departmentId',
		'department.name', //(I ADDED THIS LINE HERE.......!!!)
		'firstName',
		'lastName',
		'email',
		'ext',
		/*
		'hireDate',
		'leaveDate',
		*/
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>

This works fine because of 'relations' method in the employee model
public function relations()
	{
		return array(
			'department' => array(self::BELONGS_TO, 'Department', 'departmentId'),
		);
	}


However this column is neither sortable nor filterable in the same way that the other columns are.
Any clue how I would go about this?
And what is the general way of doing this sort of thing when tables are in relation like this?
Do I create an sql view in the database joining both tables and then run Gii on that... or is there a neater way of adapting the code I already have?
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 28 October 2012 - 01:25 AM

Dear Friend

There is an excellent wiki article.

Just go through it.

Searching and sorting by related model in CGridView
0

#3 User is offline   John McL 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-October 12

Posted 28 October 2012 - 09:41 AM

View Postseenivasan, on 28 October 2012 - 01:25 AM, said:

Dear Friend

There is an excellent wiki article.

Just go through it.

Searching and sorting by related model in CGridView


Thank you seenivasan. This explained perfectly what I needed to know. :)

I am trying out the yii framework and was banging my head against the wall about this particular issue. Nice to know this forum is responsive and accurate. Gives me faith to keep persevering with Yii
0

#4 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 29 October 2012 - 04:50 PM

Here's another one for reference as well:

Displaying, sorting and filtering HasMany & ManyMany relations in CGridView
0

#5 User is offline   John McL 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-October 12

Posted 30 October 2012 - 06:43 AM

Cheers,

I should have properly searched the wiki; now I know it's there I guess I have a lot of homework to do. Thanks again.
0

#6 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 315
  • Joined: 08-June 10
  • Location:France

Posted 03 February 2013 - 03:55 PM

I just released a behavior to help with this: Related Search Behavior
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