Diegovl
(Diego Viola1)
September 7, 2011, 7:43pm
1
Hello!
I have a table "tbl_assignment" containing "user_id" and "subject_id".
I also have a "users" table and "subjects".
More subjects can be assigned to a user.
There may be multiple records in the table "subjects" with the same "user_id" but different "subject_id".
This is my grid.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'assignment-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'subject_id',
'value' => '$data->subject->name' // lol, this is the problem
),
array(
'name' => 'name',
'value' => '$data->user->name'
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Obviously the problem is being able to print in a cell (column subject) the various subjects assigned to a user, so you have:
A user | maths, English …
User B | religion, geography, history …
What kind of query should I create? Is there any particular relationship to use? How do I see the value in the grid?
Thanks for help!
drumaddict
(Kabasakalis)
September 7, 2011, 8:47pm
2
Diegovl:
Hello!
I have a table "tbl_assignment" containing "user_id" and "subject_id".
I also have a "users" table and "subjects".
More subjects can be assigned to a user.
There may be multiple records in the table "subjects" with the same "user_id" but different "subject_id".
This is my grid.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'assignment-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'subject_id',
'value' => '$data->subject->name' // lol, this is the problem
),
array(
'name' => 'name',
'value' => '$data->user->name'
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Obviously the problem is being able to print in a cell (column subject) the various subjects assigned to a user, so you have:
A user | maths, English …
User B | religion, geography, history …
What kind of query should I create? Is there any particular relationship to use? How do I see the value in the grid?
Thanks for help!
What does $data->subject->name return?If it returns an array with all subject names,maybe you could use
implode(" , ", $data->subject->name),it would convert the array to a string,joining all elements with commas.
Just a thought.
Diegovl
(Diego Viola1)
September 7, 2011, 9:16pm
3
drumaddict:
What does $data->subject->name return?If it returns an array with all subject names,maybe you could use
implode(" , ", $data->subject->name),it would convert the array to a string,joining all elements with commas.
Just a thought.
$data->subject->name return only the first name of subject.
print_r($data->subject) will return an array with the id of subjects.
What can i do?
jowen
(Ljowen985)
September 8, 2011, 12:44am
4
Maybe you can try using CSqlDataProvider with CGridView class reference.
By the way , i always got this error PHP Error: Trying to get property of non-object when i try using code below inside CGridView. Can anyone tell me why this happens.
array(
'name' => 'name',
'value' => '$data->user->name'
),
perochak
(Amjad Mughal)
September 8, 2011, 2:01am
5
Instead of this, what I would recommended for this sort of issues or case is that
User a function in the parent model and then get the raw output or HTML output to get the list
'value'=>'Users::Courses($data->subjects)'
Now, in your static function in model Users
public static function Courses($courses){
return explode(',',$courses->name);
}
Hope you got the idea