Items custom ordering
Just like the way you customize filters in your Items object, you can also create custom orders to get ordered Item lists.
Let's say you want to be able to get movie lists ordered by the year they were released, from older to newer. You would overload the fillFromParameters method of your Items object like this:
This time, the only modification we did was to add a new ordering method to the orders
$p
parameter. Our ordering method is called released
, and the SQL part responsible of the ordering is movies.year desc
.
In the example we're using the BasicObject::treatParameters helper method that allows us to treat parameter hash arrays like $p
more easily, specially when you have lots many parameters to treat.
Without using treatParameters,
$p["orders"]["released"] = "movies.year asc";
would've done almost the same.
And that's it, now we can use the Movies class to retrieve lists of movies ordered by their release year like this:
Note that, when instantiating the Movies object, the order
key
is an array. This is because we can pass more than one orders to get lists ordered by multiple criteria at the same time. For example, ordering movies first by their release year, and then by their title. Take a look at Mixing filters and ordering for an example.
See this example working in the Cherrycake documentation examples site.
Last updated