Default ordering for your CakePHP 3 tables
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status Coverage Total Downloads License

Allows setting default order for your tables.


Using Composer:

composer require muffin/orderly

Then load the plugin using the shell command:

bin/cake plugin load Muffin/Orderly

or by manually adding statement shown below to your app's config/bootstrap.php:


What is does

Orderly allow you to set default ORDER clause for your table queries, similar to the functionality provided by Model::$order property of CakePHP 2.x.


Here's how you can add the Orderly behavior for your tables:

// By default orders by display field of table.

// Default ordering using specified field.
$this->addBehavior('Muffin/Orderly.Orderly', ['order' => $this->aliasField('field_name')]);

Value for order key can any valid value that \Cake\ORM\Query::order() takes. The default order clause will only be applied to the primary query and when no custom order clause has already been set for the query.

You can also configure the behavior to apply the default order based on required condition using callback option. The order will be applied if callback returns true:

$this->addBehavior('Muffin/Orderly.Orderly', [
    'order' => ['Alias.field_name' => 'DESC'],
    'callback' => function (\Cake\ORM\Query $query, \ArrayObject $options, $primary) {
        //return a boolean

The behavior can also be configured with multiple orders which are applied based on return value of their respective callbacks:

$this->addBehavior('Muffin/Orderly.Orderly', [
        'order' => ['Alias.field_name' => 'DESC'],
        'callback' => function (\Cake\ORM\Query $query, \ArrayObject $options, $primary) {
            //return a boolean
        'order' => ['Alias.another_field'],
        'callback' => function (\Cake\ORM\Query $query, \ArrayObject $options, $primary) {
            //return a boolean

Patches & Features

  • Fork
  • Mod, fix
  • Test - this is important, so it's not unintentionally broken
  • Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
  • Pull request - bonus point for topic branches

To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards.

Bugs & Feedback


Copyright (c) 2015, Use Muffin and licensed under The MIT License.