Kailas
(Kailas Bedarkar)
December 30, 2013, 6:56am
1
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
Kailas
(Kailas Bedarkar)
December 30, 2013, 7:52am
3
nineinchnick:
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
sorry, it’s has many i’ve write wrong.
Kailas
(Kailas Bedarkar)
December 30, 2013, 1:22pm
4
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]=>[/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]
sukunj
(Mendparasukunj27)
December 31, 2013, 10:21am
5
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
-----------------------------------------------------
In the testview.php file
$this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'testForm-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'studName',
'value'=>$data->stud->studName,
),
'testName',
'[size="2"]testResult[/size][size="2"]'[/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);