spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

Implemented support for ordering by functions #269

Closed FlipEverything closed 5 years ago

FlipEverything commented 5 years ago

It would be really nice if you could order by a built-in (or user-defined) driver function. Currently count() and group() only works with fields specified in the entity's definition because fieldAliasMapping() appends the tableName before the fieldName. If the fieldName happens to be a function then it does not work. I implemented a simple "function detector" that works with one parameter (mysql) functions. It does not work with complex functions.

This way you can do something like this:

$mapper->all()->order(['WEEK(date)' => 'ASC', 'priority' => 'DESC']);

It translates to this:

ORDER BY WEEK(table.date) ASC, priority DESC

instead of

ORDER BY table.WEEK(date) ASC, priority DESC

This is 100% backwards compatible and it does not break the field alias support. What do you guys think?

FlipEverything commented 5 years ago

I improved the "function detector" to work with complex queries. I changed the basic tests to be platform independent and added complex function tests for the three tested drivers.

FlipEverything commented 5 years ago

summon @vlucas

FlipEverything commented 5 years ago

ping

vlucas commented 5 years ago

Sorry I was in Montivideo, Uruguay on a work trip for a week. I am back now :)

FlipEverything commented 5 years ago

Thank you! Sorry, I did not mean to bother you.