Sorting On Second Attribute Of A Concatenated Virtual Attribute

I have CGridView list names where I have a column that is a full name concatenated from a first name and last name (so the full name would have the form "John Smith"). However I want the column to be sortable on the last name. I have the following for sort in my model (as well as the virtual attribute declaration):




            'sort'=>array(

                'defaultOrder'=>'last_name ASC, status_id DESC',

                'attributes' => array(

                    'full_name' => array(

                        'asc' => "last_name,first_name",

                        'desc' => "last_name,first_name DESC",

                    ),

                    '*',

                ),

            ),



I have the following in the view:




        'columns'=>array(

            array('name'=>'full_name', 

                'header'=>'Name', 

                'value'=> '$data->first_name." ".$data->last_name',

                'htmlOptions'=>array('style'=>'width: 160px')),



This only sorts full_name by the first character of full_name (the first name) however. Is there a way to do this without having to list the full name as "last_name, first_name" (ie Smith, John)?

Thanks, I’ve been searching and experimenting for hours and haven’t figured this one out.

Bob

What is your data provider? Because the solution with setting sort attributes is the right one and it should work with CActiveDataProvider.

I noticed that I had an older version of the attributes shown. Including both last_name and first_name in the sort attributes array resulted in it not sorting at all. The header acted as though it was sorting, but the names never changed order. With just last_name included in the sort attributes array (as shown below), it sorts on the first_name.

Here is my full search() function in the model.




    public function search() {

        $criteria=new CDbCriteria;

        $criteria->compare('email',$this->email,true);

        $criteria->compare('date_added',$this->date_added,true);

        $criteria->with=array('status');

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

            'sort'=>array(

                'defaultOrder'=>'last_name ASC, status_id DESC',

                'attributes' => array(

                    'full_name' => array(

                        'asc' => "last_name",

                        'desc' => "last_name DESC",

                    ),

                    '*',

                ),

            ),

        ));

    }



Thanks for looking at this.

Based on your reassurance that it should work, I reviewed the name data in the database to check for blank spaces in front of names and found one (I was only using three names for testing). With that fixed everything works fine. I’m applying trim() to all input fields now!

I appreciate you taking the time to look at my code and letting me know it should work; that really helped me. My God, the time I’ve spent trying to figure out why this wasn’t working!

Bob