-1

index.php

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
        array(
            'header' => 'User Name',
            'value' => 'CHtml::encode($data->sud_profile->user_id)',//This will use the relationship and get all the details of the paticular user from users table
        ),
        'subject',
        'message',

I have two tables i want to display names instead of ids.
My two tables are:

table 1

msg table
---------
id sender receiver subject message
------------------------------------
1   3      4         wish   have a god

table 2

--------
sud_profile
-------------
id user_id firstname lastname
1    2     kumar         rai

From these two tables i want to display names based on ids like sender and user_id.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Mallesh
  • 1
  • 2
  • add both of your models or share the relations that you have declared inside the models and the `sender` and `receiver` inside the `msg` table are holding id from the `user` table? and you want to display `name` ? or `username` for these columns as the `sud_profile` table already holding the user `first` and `last` names ? – Muhammad Omer Aslam Apr 16 '18 at 21:49
  • hey, do mark the answer as correct if it helped you out – Muhammad Omer Aslam May 16 '18 at 20:20

1 Answers1

0

You should have added the code for the respective models or add the respective relations in both of the models but i will attempt to guide you so that you could follow up according to your code.

You can call the relation defined inside the Message model to access the user and then display the username.

About msg table

i assume that the sender and receiver columns are holding the id from the user table, you should have the relations defined inside the Message model or define one now and add the below inside your relations array (change your model names and field names if different).

As you have the sender and receiver 2 columns both holding different user_id so you need to define 2 relations.

 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(
          '_sender' => array(self::BELONGS_TO, 'User', 'sender'),
          '_receiver' => array(self::BELONGS_TO, 'User', 'receiver'),
        );
    }

and then inside the GridView change your column configurations to the following.

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
    array(
        'name'=>'sender',
        'header'=>'Sender',
        'htmlOptions'=>array('data-title'=>'Sender'),
        'value'=>'$data->_sender->username'
    ),
    array(
        'name'=>'receiver',
        'header'=>'Receiver',
        'htmlOptions'=>array('data-title'=>'Receiver'),
        'value'=>'$data->_receiver->username'
    ),

))
);

About sud_profile table

You need to do the same for the sud_profile table, define the relation and display it using the relation inside the gridview.

Your relation will look like below

 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(
          '_user' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

And use the relation like below inside your gridview column configurations

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
         array(
            'name'=>'user_id',
            'header'=>'Username',
            'htmlOptions'=>array('data-title'=>'Sender'),
            'value'=>'$data->_user->username'
         ),    
    ))
);
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68