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'))); } }