How To Build Relations To Display Columns Of 4 Tables In Cgridview

150 Views Asked by At

I have a requirement where I need to build the relations between more than 3 tables. I have 4 tables namely, Message, Flat, Person, Mapping tables. Now, below tables have the following fields:

Message:

`Id` int(11) NOT NULL AUTO_INCREMENT,                
`Mapid` int(11) DEFAULT NULL,                     
PRIMARY KEY (`Id`),                                
KEY `FK41715B218022FC0` (`MapId`)                    

Mapping

`Id` int(11) NOT NULL AUTO_INCREMENT,  
`FlatId` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`),  
KEY `FKE2B3C68A24F94F50` (`FlatId`),

Flat

`Id` int(11) NOT NULL AUTO_INCREMENT,               
`PersonId` int(11) DEFAULT NULL,                    
PRIMARY KEY (`Id`),                                
  KEY `FK2FFF79122B94A6` (`PersonId`),              

Person

`Id` int(11) NOT NULL AUTO_INCREMENT,  
`Name` varchar(255) DEFAULT NULL,  
 `FlatId` int(11) DEFAULT NULL,  
`Phone` varchar(255) DEFAULT NULL,  
PRIMARY KEY (`Id`),  
KEY `FKC4E39B55AF5432C` (`FlatId`),

Now, I have to build relations in such a way that in the Cgridview(admin.php) of Message, i should display PersonId of flat table and Name and Phone of Person table along with the columns of Message table.

I have defined relations like this in model class of message(message.php)

   public function relations()
    {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    'mapping' => array(self::BELONGS_TO, 'Mapping', 'MapId'),
    'flat'=>array(self::HAS_ONE,'Flat',array('FlatId'=>'Id'),'through'=>'mapping'),
    'person'=>array(self::HAS_ONE,'Person',array('PersonId'=>'Id'),'through'=>'flat'),
    );

}

Can anyone explain me the step by step procedure to display the columns of person table in message gridview.

2

There are 2 best solutions below

8
On

You can pass a DataProvider to the view and use it in CgridView widget or use a 'search()' action from $model.

You can personalize the relation columns like this:

<?php 
$this->widget('zii.widgets.grid.CGridView',array(
    'id'=>'messagePerson-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'Id',
        'Mapid',
        array(
            'header'=>'Person Name', // Personalize column name
            'value'=>'$data->flat->person->Name',
            'htmlOptions'=>array('style'=>'width:10%;'), // Personalize html attributes
        ),
        'flat.person.FlatId', // Or directly with default relation name.
        'flat.person.Phone',
    ));
?>
0
On

With assuming you can create dataProvider for the CGridView:

<?php 
$this->widget('zii.widgets.grid.CGridView',array(
    'id'=>'message-grid',
    'dataProvider'=>$yourDataProvider //such as $model->search();
    'filter'=>$model,
    'columns'=>array(
        'Id',
        'Mapid',
        'person.name',
        'person.FlatId',
        'person.Phone',
    ));
?>