Cherrycake
ExamplesGithub
version 2.x alpha
version 2.x alpha
  • Introduction
  • Status
  • Changelog
  • Migration
  • Architecture
    • Basics
    • Modules
    • Classes
    • Lifecycle
      • Deep lifecycle
    • Performance
    • Security
    • Patterns
      • Files structure
    • Items
    • Server requirements
  • Guide
    • Getting started
      • Skeleton start
      • Docker start
    • Modules guide
    • Classes guide
    • Actions guide
      • Complex actions
      • Variable path components
      • Accept GET or POST parameters
      • Getting the URL of an action
      • Cached actions
      • Brute force attacks
    • Patterns guide
      • Passing variables to a pattern
      • Nested patterns
      • Cached patterns
    • Cache guide
      • Time To Live
      • Using cache
      • Lists
      • Queues
      • Pools
    • Database guide
      • Basic queries
      • Prepared queries
      • Cached queries
      • Cache key naming
      • Removing queries from cache
    • Items guide
      • Item cache
      • Item lists
      • Items custom filters
      • Items custom ordering
      • Mixing filters and ordering
      • Items with relationships
      • Items cache
    • HtmlDocument guide
    • Css and Javascript guide
      • Modules injecting CSS and JavaScript
    • Session guide
    • Login guide
      • Creating a complete login workflow
    • Locale guide
      • Multilingual texts
      • Domain based site localization
    • Log guide
      • Loading Log events from the database
    • Stats guide
      • Stats events with additional dimensions
      • Loading Stats events from the database
    • Janitor guide
      • Janitor tasks configuration files
    • Command line interface
    • Debugging
  • Reference
    • Core modules
      • Actions
        • Actions methods
      • Browser
      • Cache
        • Cache methods
      • Css
        • Css methods
      • Database
      • Email
      • Errors
      • HtmlDocument
        • HtmlDocument methods
      • ItemAdmin
      • Janitor
        • Janitor methods
      • Javascript
        • Javascript methods
      • Locale
        • Locale methods
      • Log
        • Log methods
      • Login
        • Login methods
      • Output
        • Output methods
      • Patterns
        • Patterns methods
      • Security
        • Security methods
      • Session
        • Session methods
      • Stats
        • Stats methods
      • SystemLog
      • TableAdmin
      • Translation
      • Validate
    • Core classes
      • Action
        • Action methods
        • Action properties
      • AjaxResponseJson
      • BasicObject
        • BasicObject methods
      • CacheProvider
        • CacheProvider methods
      • Color
      • DatabaseProvider
        • DatabaseProvider methods
      • DatabaseResult
        • DatabaseResult methods
        • DatabaseResult properties
      • DatabaseRow
      • Engine
        • Engine methods
        • Engine properties
      • Gradient
      • Item
        • Item methods
        • Item properties
      • Items
        • Items methods
        • Items properties
      • Image
      • JanitorTask
        • JanitorTask methods
        • JanitorTask properties
      • LogEvent
        • LogEvent methods
        • LogEvent Properties
      • LogEvents
        • LogEvents methods
      • Module
        • Module methods
        • Module properties
      • Response
      • Request
        • Request methods
      • RequestParameter
        • RequestParameter methods
      • RequestPathComponent
        • RequestPathComponent methods
      • Result
      • StatsEvent
        • StatsEvent properties
      • StatsEvents
        • StatsEvents methods
      • SystemLogEvent
        • SystemLogEvent methods
        • SystemLogEvent properties
      • SystemLogEvents
        • SystemLogEvents methods
  • Code conventions
  • License
  • Extras
Powered by GitBook
On this page

Was this helpful?

  1. Guide
  2. Items guide

Items custom ordering

PreviousItems custom filtersNextMixing filters and ordering

Last updated 5 years ago

Was this helpful?

Just like the way you 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 object like this:

class Movies extends \Cherrycake\Items {
    protected $tableName = "movies";
    protected $itemClassName = "\CherrycakeApp\Movie";
    
    function fillFromParameters($p = false) {
        // Treat parameters
        self::treatParameters($p, [
            "orders" => ["addArrayKeysIfNotExist" => [
                "released" => "movies.year asc"
            ]]
    		]);
        
        // Call the parent fillFromParameters
        return parent::fillFromParameters($p);
    }
}

This time, the only modification we did was to add a new ordering method to the $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 helper method that allows us to treat parameter hash arrays like $p more easily, specially when you have lots many parameters to treat.

And that's it, now we can use the Movies class to retrieve lists of movies ordered by their release year like this:

$movies = new Movies([
    "fillMethod" => "fromParameters",
    "p" => [
        "order" => ["released"]
    ]
]);

foreach ($movies as $movie)
    echo "{$movie->title} ({$movie->year})\n";
The Day the Earth Stood Still (1951)
The War of the Worlds (1953)
2001: A Space Odyssey (1968)
Planet of the Apes (1968)
Silent Running (1972)
Close Encounters of the Third Kind (1977)
Alien (1979)
The Thing (1982)
Blade Runner (1982)
Tron (1982)
E.T. the Extra-Terrestrial (1982)
Brainstorm (1983)
WarGames (1983)
Dune (1984)
The Last Starfighter (1984)
Enemy Mine (1985)
Explorers (1985)
The Abyss (1989)
Contact (1997)
The Hitchhiker’s Guide to the Galaxy (2005)
War of the Worlds (2005)
The Man from Earth (2007)
Moon (2009)
Super 8 (2011)
Prometheus (2012)
Interstellar (2014)
Ex Machina (2014)
The Martian (2015)
Arrival (2016)
Blade Runner 2049 (2017)

Without using , $p["orders"]["released"] = "movies.year asc"; would've done almost the same.

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 for an example.

See this example working in the site.

Mixing filters and ordering
Cherrycake documentation examples
customize filters
Items
treatParameters
BasicObject::treatParameters
orders