Fix 'order' not working with a single expressions

This commit is contained in:
chinpei215 2017-11-29 00:17:57 +09:00
parent 668e7473b9
commit 4ae9f13dfd
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
*