Show Relational Data In Cgridview

Hi,

I’ve two tables

test and student

showing test result in grid view

table test

testID - > PK

testName

studID -> FK

testResult

table student

studID

studName

test model class relation




public function relation(){

		return array(

			'stud' => array(self::BELONGS_TO, 'StudentForm','studID'),

		);

}




student model class relation




public function relation(){

		return array(

			'exam' => array(self::HAS_MANY, 'TestForm','studID'),

		);

}



testView.php




<?php 

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

	'id'=>'testForm-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'stud.studName', 

		'testName',

		'[size="2"]testResult[/size][size="2"]'[/size]

	),

)); ?>



But the 1st column(student name is blank in grid view) of grid view row values is blank like below table





-----------------------------------------------------

Stud Name | Test Name | Test Result |

-----------------------------------------------------

             | Test1     	|  Pass

-----------------------------------------------------



You mixed up the relations. You defined exam in student as BELONGS_TO but it should be HAS_MANY. That makes it an array. Other than that, it looks fine. You’re sure the data in your database is correct? That is, does the keys match?

BTW. You know what ‘stud’ means? Don’t be lazy and use proper naming :slight_smile:

sorry, it’s has many i’ve write wrong.

i’ve find the solution wrong spell [size=2]relation->[/size][size=2]relations[/size]

[color=#000088]public[/color][color=#000000] [/color][color=#000088]function[/color][color=#000000] relations[/color]color=#666600{[/color][color=#000000]

            [/color][color=#000088]return[/color][color=#000000] array[/color][color=#666600]([/color][color=#000000]


                    [/color][color=#008800]'exam'[/color][color=#000000] [/color][color=#666600]=&gt;[/color][color=#000000] array[/color][color=#666600]([/color][color=#000088]self[/color][color=#666600]::[/color][color=#000000]HAS_MANY[/color][color=#666600],[/color][color=#000000] [/color][color=#008800]'TestForm'[/color][color=#666600],[/color][color=#008800]'studID'[/color][color=#666600]),[/color][color=#000000]


            [/color][color=#666600]);[/color][color=#000000]

[/color][color=#666600]}[/color]

In the testview.php file

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

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


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


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


'columns'=&gt;array(


	array(


             'name'=&gt;'studName',


             'value'=&gt;&#036;data-&gt;stud-&gt;studName,


             ),


	'testName',


	'[size=&quot;2&quot;]testResult[/size][size=&quot;2&quot;]'[/size]


),

)); ?>

first declare $public studName;

then add this attributes in serach and safe attribute array

and in ur search method use

$criteria->with = array(‘stud’);

$criteria->compare(‘stud.studName’,$this->studName,true);