1
votes

How to creat a multi model CGridView in Yii framework. I've searched documentation of Yii.

Below are the two tables for example of what i needed

Table: user_master

+---------+----------+-------------+-------------+
| user_id | name     | email       | active      |  
|   1     | Darshit  | [email protected] | true        |  
|   2     | Obed     | [email protected] | true        |  
|   3     | abmed    | [email protected] | true        |  
|   4     | clutch   | [email protected] | true        |  
|   5     | sirisb   | [email protected] | true        |  
+---------+----------+-------------+-------------+

Table: friend_master

+---------+-----------+--------------+-------------+  
| user_id | friend_id | date_created | is_deleted  |  
+---------+-----------+--------------+-------------+    
|   1     |    2      | 2011-12-14   | false       |  
|   1     |    5      | 2011-12-14   | false       |  
|   3     |    5      | 2011-12-14   | false       |  
|   2     |    4      | 2011-12-14   | false       |  
|   1     |    3      | 2011-12-14   | false       |  
|   5     |    2      | 2011-12-14   | false       |  
+---------+-----------+--------------+-------------+  

I want grid like this:

Desired Result

I've created and models of both table but they only display single model grid view.

So, I'm not getting the way to do it. Any help will be highly appreciable.

1

1 Answers

4
votes

It looks like you do not need a multimodel gridview. The grid that you give as needed is displaying elements of just one model, user_master. The property "Friends" actually belongs to that model, because it's a value related to each and every particular instance of the user_master table. So you can actually imagine the property "Friends" as an extra column in the user_master table. You won't need to add that row to the table, just to declare as a property in the model. Add the following row to the array returned by the 'relations' function of the user_master model:

'FriendsCount'=> array(self::STAT, 'friend_master', 'user_id'),

You can read more about how this works here: http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#relations-detail.

Briefly explained, the STAT type of relation simply retrieves statistical values of the model. In this particular case, it will only count the number of friends related to each instance of the user_master model.

Now, inside your CGridView definition, you must declare the 3 columns you want to display. This is done inside the 'columns' array. Of course, 'user_id' and 'name' will be recognized as explicit-declared properties of the model being displayed, so we don't need to care about them. But for the 'Friends' column you may need to explicitly specify the value we want to display. This is done simply using an array:

<?php $this->widget('zii.widgets.grid.CGridView', array(
        ...
    ...
    'columns'=>array(
        'user_id',
        'name',
             array(
               'class'=>'CDataColumn',
           'name'=>'Friends',
           'value'=>'$data->FriendsCount',  //The FriendsCount relation we declared                              
             ),
    ),
)); 
?>

You may notice that using this array-based syntax, you can declare quite versatile columns.

I hope you find this post helpful.