From 7250f1b04471ef3b575688fca47db610e4f0e5df Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 5 Feb 2016 11:56:20 +0530 Subject: [PATCH] Fix error thrown when query already has order clause. --- src/Model/Behavior/OrderlyBehavior.php | 5 +++-- .../Model/Behavior/OrderlyBehaviorTest.php | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Model/Behavior/OrderlyBehavior.php b/src/Model/Behavior/OrderlyBehavior.php index 7febab6..04cc33d 100644 --- a/src/Model/Behavior/OrderlyBehavior.php +++ b/src/Model/Behavior/OrderlyBehavior.php @@ -37,8 +37,9 @@ public function beforeFind(Event $event, Query $query, ArrayObject $options, $pr $args = [$query, $options, $primary]; foreach ($orders as $config) { - if ((empty($config['callback']) && !$query->clause('order')) - || call_user_func_array($config['callback'], $args) + if ((!empty($config['callback']) + && call_user_func_array($config['callback'], $args) + ) || !$query->clause('order') ) { $query->order($config['order']); break; diff --git a/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php b/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php index 8f9bacb..4de17b4 100644 --- a/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php @@ -104,6 +104,18 @@ public function testBeforeFind() $event = new Event('Model.beforeFind', $this); $query = $this->Table->query(); $behavior->beforeFind($event, $query, new \ArrayObject, true); - debug($query->clause('order')); + $this->assertEquals(1, count($query->clause('order'))); + } + + public function testBeforeFindQueryWithOrder() + { + $this->Table->addBehavior('Muffin/Orderly.Orderly'); + $behavior = $this->Table->behaviors()->Orderly; + + $event = new Event('Model.beforeFind', $this); + $query = $this->Table->find() + ->order('author_id'); + $behavior->beforeFind($event, $query, new \ArrayObject, true); + $this->assertEquals(1, count($query->clause('order'))); } }