GridView and Group by

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.

$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?

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'

        ),

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