Yii Framework Forum: Cgridview -> Sort By Virtual Attribute - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cgridview -> Sort By Virtual Attribute Rate Topic: -----

#1 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 12:01 PM

Well, this is my "Users" model:

public $fullname;

{other code here - not really important for this problem}

public function search()
{

         $sort = new CSort();
         $sort->attributes = array('*', 'CONCAT(surname, ", ", forename) AS fullname');
         $sort->defaultOrder = array('fullname' => CSort::SORT_ASC);

$criteria=new CDbCriteria;
         $criteria->select = array('*', 'CONCAT(surname, ", ", forename) AS fullname');
         $criteria->compare('id', $this->id);
$criteria->compare('login', $this->login, true);
         $criteria->compare('surname', $this->surname, true);
         $criteria->compare('forename', $this->forename, true);
         $criteria->compare('email', $this->email);
$criteria->compare('CONCAT(surname, ", ", forename)', $this->fullname, true);
$criteria->compare('company_id', $this->company_id);
$criteria->compare('grupa', $this->grupa);
$criteria->compare('active', $this->active);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
    'pagination'=>array('pageSize'=>10),
    'sort' => $sort,
));
}


The CGridView widget generated with such a data provider works fine but for whatever reason "Fullname" column is not sortable. Can anybody here give me a clue why this problem occures???

Thanks ahead!
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,047
  • Joined: 16-February 11
  • Location:Japan

Posted 29 December 2012 - 02:27 PM

Hi Ziggi,

Did you try this?
         $sort->attributes = array('*', 'fullname'');

0

#3 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 02:31 PM

Yes, of course.

And I believe I traced the origin of the problem. You may look here for details:

https://github.com/y...yii/issues/1899

Rgs,
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 December 2012 - 02:39 PM

Dear Friend

I hope the following is helpful.

$sort->attributes = array('*', "fullname"=>array(
"asc"=>"CONCAT(surname, ", ", forename) ASC",
"desc"=>"CONCAT(surname, ", ", forename) DESC",
));


OR just do this.
$sort->attributes = array('*', "fullname"=>array());


High Regards.
0

#5 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 02:48 PM

Dear Standar Member "seenivasan" - I strongly encurage you to validate your "solutions" prior posting. Yet another time you drop your unfortunately not-really-helpful advice simply because you do not have a habit to test your "solutions"!

Now please (high regards) and read my bug report:

https://github.com/y...yii/issues/1899
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 December 2012 - 02:56 PM

Dear Friend

That is why I avoided the virtual attribute fullname in values in declaration of attributes .
The key contains the virtual attribute.
I tested the things prior to posting.
I doubly cautious.
1

#7 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 03:07 PM

Dear seenivasan - I tested your solution and this is not working - the column header of the CGridView is not a link, and it is not possible to click on this and reverse the sort order etc.

CORRECTED:

Indeed - it is now working!
I made a typo - wrote "fulname" not "fullname'

Argh - I apologize!
0

#8 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 December 2012 - 03:13 PM

Dear Friend

This is my code to simulate your scenario.

Model

public $fullname;
public function rules()
	{
		
		return array(
			array('fname, lname', 'required'),
			array('fname, lname', 'length', 'max'=>64),
			// Please remove those attributes that should not be searched.
			array('id, fname, lname,fullname', 'safe', 'on'=>'search'),
		);
	}


public function search()
	{
		
		$criteria=new CDbCriteria;
                $criteria->select="*,CONCAT(fname,'-',lname) AS fullname";
		$criteria->compare('id',$this->id);
		$criteria->compare('fname',$this->fname,true);
		$criteria->compare('lname',$this->lname,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			"sort"=>array('attributes'=>array("*",'fullname'=>array(
			'asc'=>"CONCAT(fname,'-',lname)  ASC",
			'desc'=>"CONCAT(fname,'-',lname)  DESC",
			
			))),
		 // "sort"=>array('attributes'=>array('id'.'fname','lname','fullname'=>array()))
		));
	}


admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'family-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'id',
		'fname',
		'lname',
		'fullname',
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>


Just working fine.
You are my friend forever.
1

#9 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 358
  • Joined: 23-July 09

Posted 29 December 2012 - 03:13 PM

Further to your problem with virtual attributes, it only gets worse when the attribute is an array (eg.multiple languages from a related table).

I will be interested to see the response to your bug report, please post here when they do.
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#10 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 03:17 PM

Yeap seenivasan,

You are right - I was wrong! I made a typo.
Apologize once again.
0

#11 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,047
  • Joined: 16-February 11
  • Location:Japan

Posted 29 December 2012 - 03:23 PM

Do you have a getter function for fullname?
If you have getFullname() or getFullname(), then it would interfere with the virtual attribute of "fullname", IIRC.
0

#12 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 29 December 2012 - 03:29 PM

Definitely, getter function is completely irrelevant in this point.
It was the beginning of my reseach and my conclusion was... irrelevant!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users