Search on concatenated columns in CGridView

I want to present three columns from a table called Customers in a CGridView, the columns are id, name and email.

This is the code I’ve done so far:

<?php $this -> widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $model -> search(),

    'filter' => $model,

    'columns' => array(


            'header' => 'Kundid',

            'name' => 'id',

            'type' => 'html',

            'value' => 'l($data -> id, array("customer/view", "id" => $data -> id))',



            'name' => 'name',

            'filter' => CHtml::activeTextField($model, ''),




            'class' => 'CButtonColumn',

            'template' => '{update} {delete}',


    ),)); ?>

The name column, is retrieved by a getter method in the Customer model that looks like this:

public function getName()


    return $this -> firstName .' '. $this -> lastName;


The problem is that I want this concatenated column to be both searchable and sortable as any other column in the CGridView, I suspect that a new attribute has to be created for the Customer model and then added to the search criteria within the search() method but I have tried this with no success.

Hopefully someone could shed some light on this one for me, thanks in advance!

You guessed the path.


public $name;

at the top of the file, then in the rules add:

array('... , name', 'safe', 'on'=>'search')


and in the search condition add something like:

$criteria->addCondition('( firstName like "%:name%" or lastName like "%:name%")');

$criteria->params= array(':name'=>$this->name);

[font="arial, verdana, tahoma, sans-serif"][size="2"]First off I omitted the search scenario when creating the model in the controller action, now there it made a solution possible.[/size][/font]

[font="arial, verdana, tahoma, sans-serif"][size="2"]

Secondly I tried the last snipped of your code zaccaria but never got it to work, without analyzing it further I got it to work with this code (where fullName corresponds to the name variable in zaccarias example):[/size][/font]

[font="arial, verdana, tahoma, sans-serif"] [/font]

[font="arial, verdana, tahoma, sans-serif"][size="2"]

$criteria -> compare('CONCAT_WS(" ", firstName, lastName)', $this -> fullName, true);


Hi, i have also a problem like this but on the sort function. Can you please help me. Thank you :)

Please contribute with the code and perhaps a description of the problem.

For set the sort you should exit the property DataProvider=>Sort=>attributes