Pages

Sunday, June 9, 2013

Cakephp-Sort by custom made field in MYSQL not working in pagination

Recently in one of my cakephp projects.I came across a issue in pagination and sorting of data on basis of a custom made field..

Case:
I had a MYSQL field "video_length" in STRING which should have been in TIME(DB was not created by me)...

My requirement was sort on basis of this field so in my find function of Model I used

"TIME_TO_SEC(UserVideo.video_length)" AS vid_len and sorted by the same

It was working fine in normal scenario but was not showing sorted results under pagination may be because it was not permanent column of table

Solution:

I did some tricky thing here..I needed the column in format TIME_TO_SEC so in my fields property in my find function I customized fields and added concerned field in name of old field sth like

$this->UserVideo->find('all',array('fields'=>array("TIME_TO_SEC(UserVideo.video_length)" AS video_length)))

See words marked in bold

And finally all was coming well under pagination as well

No comments:

Post a Comment