Toby
Toby

Reputation: 8792

Advanced Sort in CakePHP

I need to amend a system built in cakePHP to change the sort order from alphabetical on a field to alphabetical on the same field but minus the first character (whilst still displaying the entire field).

So essentially if they had.. a1, a3, b2, b4 the sort order would be a1, b2, a3, b4

The code currently sorts based on find() is there a way I can force this type of sort into it or do I need to write some type of custom query?

Upvotes: 1

Views: 1245

Answers (1)

pleasedontbelong
pleasedontbelong

Reputation: 20102

well.. you have two options:

Sort it using php after the query. You could use usort. But this wont work if you want to paginate the results

Or you could modify the find query to something like this:

$this->MyModel->find('all',array(... ,'order'=>'SUBSTRING(MyTable.sortfield,2) ASC'));

the idea is to generate a query like:

SELECT * FROM `MyTable` WHERE ... ORDER BY SUBSTRING(sortfield,2);

Good Luck

Upvotes: 3

Related Questions