Merge pull request #11472 from chinpei215/2.x-order-expression

[2.x] Fix 'order' not working with a single expressions
This commit is contained in:
Mark Story 2017-11-28 14:59:07 -05:00 committed by GitHub
commit 979eaeef5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View file

@ -3083,7 +3083,11 @@ class Model extends CakeObject implements CakeEventListener {
$query['order'] = $this->order;
}
if (is_object($query['order'])) {
$query['order'] = array($query['order']);
} else {
$query['order'] = (array)$query['order'];
}
if ($query['callbacks'] === true || $query['callbacks'] === 'before') {
$event = new CakeEvent('Model.beforeFind', $this, array($query));

View file

@ -7423,7 +7423,7 @@ class ModelReadTest extends BaseModelTest {
}
/**
* Test find(count) with Db::expression
* Test find(count) with DboSource::expression
*
* @return void
*/
@ -7445,6 +7445,38 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(1, $result);
}
/**
* Test 'order' with DboSource::expression
*/
public function testOrderWithDbExpressions() {
$this->loadFixtures('User');
$User = new User();
$results = $User->find('all', array(
'fields' => array('id'),
'recursive' => -1,
'order' => $this->db->expression('CASE id WHEN 4 THEN 0 ELSE id END'),
));
$expected = array(
array(
'User' => array('id' => 4),
),
array(
'User' => array('id' => 1),
),
array(
'User' => array('id' => 2),
),
array(
'User' => array('id' => 3),
),
);
$this->assertEquals($expected, $results);
}
/**
* testFindMagic method
*