Skip to content

Commit

Permalink
Fix error thrown when query already has order clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 5, 2016
1 parent f7afaf3 commit 7250f1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Model/Behavior/OrderlyBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
}

0 comments on commit 7250f1b

Please sign in to comment.