Merge Two Or More Columns In Gridview

Hi,

I have tried to merge two columns for Gridview using examples from yii 1.1, which doesn’t work.

I am getting the error like getting unknown property.

I have created a function in Model like:




public static function getFull_Name()

        {

                return $this->first_name . ' ' . $this->last_name;

        }



I have also created a public property in the Model class like:




public $Full_name



and for gridview:




['label'=>'Name',

 'value'=>'$data->getFull_Name()',                      

  ],



I am very new to Yii or Yii2.

My impression of Yii2 is that it is very powerful framework.

Having said that it is very difficult for a person, who has not enough experience in using PHP framework.

Reason:

[list=1]

[*]Even the most used functions are not presented with example syntax, even though there is a exhaustive documentation.

[*]This is third instance I got stuck for seemingly trivial issue.

[*]I feel that a small application using the basic functions should be there as a demo, so that for most people it will be a pleasing experience and avoid frustration and start looking somewhere else as if the learning curve for Yii is very stiff.

[*]Though I think that in fact it is not. The reason is there is so much documentation for the experts, but there is too little for the average users and that is evident from the fact from the type of questions in the forum.

[/list]

I am not complaining but stating what I feel and communicating what impression what I got initially.

Syntax in yii2 is different. You must use it like this:




[

'label'=>'Full_name',

 'value'=>function($data) {

    return $data->full_Name;

}                      

  ],



Thank you so much. That works fine.

But where are those syntax mentioned? Where I can refer them?

Again why is it full_Name and not Full_Name, whereas I have created the function getFull_Name.

Any way thanks again.

@Pawan you need to create calculated grid column values through Closure as described by kalempir earlier.

Anyway if you still intend using a getter function directly as an attribute in your grid column, you must not use a static function for the getter - instead you should be creating a normal public function for retrieving it in the model context. So change your function to something like




public function getFull_Name()

{

   return $this->first_name . ' ' . $this->last_name;

}



which you can directly refer in your grid view as an attribute which is not case sensitive (however note if using it this way sorting/filtering will not work):




[

    'label'=>'Name',

    'attribute'=>'Full_Name',

],



Thanks Kartik

Then how I can make the filtering work as well? What is the best approach to do the same?

Change your data provider to load data from related models (check loading relational data section of the yii-2 docs).

@Kartik

But here I am using the data from the same model for merge, does it still mean relational data in yii2?

No you don’t need relations then. In such cases (even for relational data), you can add a non-database field attribute (make it safe) to your model (e.g. $full_name). Initialize this field to the right value in your model (concatenating the names) which can be set before the grid renders.

Then just edit the search function in your SearchModel to add the right query condition using first_name, last_name, and full_name. This should enable filtering. Something like:




public function search($params) {

...

$query->andWhere(

    'first_name LIKE "%' . $this->full_name . '%" ' .

    'OR last_name LIKE "%' . $this->full_name . '%"'

);

...

}



@Kartik

I have made change in the model like




public $full_name;


public function rules()

	{

		return [

..

[['full_name'],'safe'],

..


public function getFull_Name()

        {

               return $this->first_name. ' ' .$this->last_name;

               

        }




and my gridview code like:


       [

       'label'=>'Full Name',

       'attribute'=>'Full_Name',

       'value'=>'Full_Name',                            

       ],

also in the search model


public function search($params)

	{

        ..

        $query->andWhere(

                'first_name LIKE "%' . $this->full_name . '%" ' .

                'OR last_name LIKE "%' . $this->full_name . '%"'

          );

But sorting still doesn’t work and search fields, only first five fields from the model are showing.

I had my search model not activated. I un-commented the line to show it, but the problem is the search fields are showing for the first five attributes only.

You will need to do it differently in the User and UserSearch model.

I have created a wiki for this… refer it here.