Showing A Child Of A Child In Cgridview

Greetings all,

I’ve spent two days on this and can’t figure it out. I’ve also searched forums and can’t find anything that details exactly what I need. I’m very new to yii, so the explanations that are close aren’t quite working for me.

I want to display a child of a child in cgridview. I have a table called Project that has a field called KeyTasks. This field is meant to hold the key of a task (it’s the next task due). The tables layout like this:

table Project

Project.KeyProject (primary)

Project.ProjectName

Project.KeyTasks

table ProjectTasks

ProjectTasks.KeyTasks (primary)

ProjectTasks.KeyTask

table ProjectTask (this is a lookup table, holding the descriptions of the tasks)

ProjectTask.KeyTask (primary)

ProjectTask.Description

In the grid, I want to show the Project.ProjectName, ProjectTask.Description, which links to ProjectTasks. Using relations I can show ProjectTasks.KeyTasks or ProjectTasks.KeyTask no problem, but I want the child of ProjectTasks, not the child of Project, so I can show ProjectTask.Description.

Make sense? Here’s the not-working code:

Project.php model:

Relations:

	return array(


		'keyTaskStatus' => array(self::BELONGS_TO, 'ProjectTasks', 'KeyTaskStatus'),


		'keyPaymentType' => array(self::BELONGS_TO, 'PaymentType', 'KeyPaymentType'),


		'keyCustomer' => array(self::BELONGS_TO, 'Customer', 'KeyCustomer'),


		'keyDocument' => array(self::BELONGS_TO, 'Documents', 'KeyDocument'),


		'keyEmployee' => array(self::BELONGS_TO, 'Employee', 'KeyEmployee'),


		'keyProjectHealth' => array(self::BELONGS_TO, 'ProjectHealth', 'KeyProjectHealth'),


		'did' => array(self::BELONGS_TO, 'Domain', 'DID'),


	);		

Search:

	$criteria=new CDbCriteria;


	// this is so those two tables are included in the SQL generated:


	$criteria->with = array('keyCustomer', 'keyEmployee');


	$criteria->compare('KeyProject',$this->KeyProject);


	$criteria->compare('ProjectName',$this->ProjectName,true);


	$criteria->compare('keyCustomer.LastName',$this->CustomerLastName,true);


	$criteria->compare('keyEmployee.LastName',$this->SalespersonLastName,true);


	$criteria->compare('keyAddress.Street1',$this->CustomerAddress,true);


	$criteria->compare('KeyDocument',$this->KeyDocument);


	$criteria->compare('KeyPaymentType',$this->KeyPaymentType);


	$criteria->compare('KeyProjectHealth',$this->KeyProjectHealth);

Grid:

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

'id'=&gt;'project-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	array(


		'name'=&gt;'KeyProject',


		'htmlOptions'=&gt;array('width'=&gt;'40px'),


	),


	'ProjectName',


	'CustomerLastName',


	array(


		'name'=&gt;'CustomerAddress',


		'filter'=&gt;false,


	),


	'SalespersonLastName',


	'KeyTaskStatus',


	array(


		'name'=&gt;'StatusDescription',


		'value'=&gt;'KeyTaskStatus-&gt;KeyTask-&gt;Description' ,


	),


	'KeyDocument',


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>

Thanks in advance for any advice!!

Okay I’ve made some changes that I think make more sense:

Project model relations:

	return array(


		'keyTaskStatus' =&gt; array(self::BELONGS_TO, 'ProjectTasks', 'KeyTaskStatus'),


	    'keyTaskDescription' =&gt; array(ProjectTasks::BELONGS_TO, 'ProjectTask', 'keyTask'),


		'keyPaymentType' =&gt; array(self::BELONGS_TO, 'PaymentType', 'KeyPaymentType'),


		'keyCustomer' =&gt; array(self::BELONGS_TO, 'Customer', 'KeyCustomer'),


		'keyDocument' =&gt; array(self::BELONGS_TO, 'Documents', 'KeyDocument'),


		'keyEmployee' =&gt; array(self::BELONGS_TO, 'Employee', 'KeyEmployee'),


		'keyProjectHealth' =&gt; array(self::BELONGS_TO, 'ProjectHealth', 'KeyProjectHealth'),


		'did' =&gt; array(self::BELONGS_TO, 'Domain', 'DID'),


	);		

Project model search():

	&#036;criteria=new CDbCriteria;


	// this is so those two tables are included in the SQL generated:


	&#036;criteria-&gt;with = array('keyCustomer', 'keyEmployee', 'keyTaskDescription.Description');


	&#036;criteria-&gt;compare('KeyProject',&#036;this-&gt;KeyProject);


	&#036;criteria-&gt;compare('ProjectName',&#036;this-&gt;ProjectName,true);


	&#036;criteria-&gt;compare('keyCustomer.LastName',&#036;this-&gt;CustomerLastName,true);


	&#036;criteria-&gt;compare('keyEmployee.LastName',&#036;this-&gt;SalespersonLastName,true);


	&#036;criteria-&gt;compare('keyAddress.Street1',&#036;this-&gt;CustomerAddress,true);


	&#036;criteria-&gt;compare('KeyDocument',&#036;this-&gt;KeyDocument);


	&#036;criteria-&gt;compare('KeyPaymentType',&#036;this-&gt;KeyPaymentType);


	&#036;criteria-&gt;compare('KeyProjectHealth',&#036;this-&gt;KeyProjectHealth);

Now the error I get is:

Relation "Description" is not defined in active record class "ProjectTask".

But that model was created by the Model Generator and it looks right.

Okay, nevermind, I’ve figured it out :slight_smile: I had to think harder about it to write the forum post, which lead me to look at some other things and now it’s working. For anyone interested in how to do this, I recommend, um, reading the manual, which is what I finally did :slight_smile:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr