sher
sher

Reputation: 21

how to display two models in one CGridView

I have two tables, 1. Students 2.SrkParents.

relations in Students model is,

'srkParents' => array(self::HAS_MANY, 'SrkParents', 'studentID'),

relations in SrkParents model is,

'student' => array(self::BELONGS_TO, 'Students', 'studentID'),

view is,

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'students-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array('studentID','status','del',

array('name'=>'SrkParents.fathers_name',
'value'=>'$data->Students->fathers_name',
'type'=>'raw'),
array('class'=>'CButtonColumn',),),));

I dont know where i did mistake.. I get the error as shown below,

Property "Students.Students" is not defined. 

Upvotes: 0

Views: 706

Answers (3)

Sudhanshu Saxena
Sudhanshu Saxena

Reputation: 1199

Modified Answer: Just change the relation has_one from has_many

'srkParents' => array(self::HAS_ONE, 'SrkParents', 'studentID'),

Upvotes: 1

ineersa
ineersa

Reputation: 3445

Your relation name is student and this is case sensetive.

Try to change $data->Students->fathers_name to $data->student->fathers_name.

Property Students.Students is not defined.

What am I doing wrong?

  • you trying to get field Students from model Students which is not defined.

Upvotes: 0

Michael H&#228;rtl
Michael H&#228;rtl

Reputation: 8607

Your relation srkParents is a HAS_MANY relation. Thus $data->srkParents is an array of SrkParents and not an object. Which makes sense, because your students have several parents.

I don't know enough about your model attributes, so i can only guess: One way to solve this would be to add a geter to your Students model which loops over $this->srkParents to find the father object and return the name from there:

public function getFatherName()
{
    foreach($this->srkParents as $parent)
        if($parent->sex=='m' /* just guessing */)
            return $parent->name;
}

Then you could use $data->fatherName in your grid view.

Upvotes: 0

Related Questions