Skip to content

Commit f0d296b

Browse files
committed
add SQL style of Model ordering syntax
using SqlCompatible behavior, you can use $order = 'History.created DESC'; close #20
1 parent 739b004 commit f0d296b

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

models/behaviors/sql_compatible.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ public function afterFind(&$Model, $results, $primary) {
109109
* @access public
110110
*/
111111
public function beforeFind(&$Model, $query) {
112+
if (is_array($query['order'])) {
113+
$this->_translateOrders($Model, $query['order']);
114+
}
112115
if (is_array($query['conditions']) && $this->_translateConditions($Model, $query['conditions'])) {
113116
return $query;
114117
}
115-
return true;
118+
return $query;
116119
}
117120

118121
/**
@@ -132,6 +135,28 @@ public function convertDates(&$results) {
132135
}
133136
}
134137

138+
139+
/**
140+
* translateOrders method
141+
* change order syntax from SQL style to Mongo style
142+
*
143+
* @param mixed $Model
144+
* @param mixed $orders
145+
* @return void
146+
* @access protected
147+
*/
148+
protected function _translateOrders(&$Model, &$orders) {
149+
if(!empty($orders[0])) {
150+
foreach($orders[0] as $key => $val) {
151+
if(preg_match('/^(.+) (ASC|DESC)$/i', $val, $match)) {
152+
$orders[0][$match[1]] = $match[2];
153+
unset($orders[0][$key]);
154+
}
155+
}
156+
}
157+
}
158+
159+
135160
/**
136161
* translateConditions method
137162
*
@@ -267,4 +292,4 @@ protected function _translateOperator($Model, $operator) {
267292
}
268293
return '';
269294
}
270-
}
295+
}

tests/cases/behaviors/sql_compatible.test.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,66 @@ public function testFindConditionIn() {
294294
}
295295

296296

297+
/**
298+
* Order method
299+
*
300+
* @return void
301+
* @access public
302+
*/
303+
public function testOrderDESC() {
304+
$expected = array(20, 19);
305+
$result = $this->Post->find('all', array(
306+
'conditions' => array('title >' => 18),
307+
'fields' => array('_id', 'title'),
308+
'order' => array('title DESC')
309+
));
310+
$result = Set::extract($result, '/Post/title');
311+
$this->assertEqual($expected, $result);
312+
313+
$order = array(array('title' => 'DESC'));
314+
$this->assertEqual($order, $this->Post->lastQuery['order']);
315+
}
316+
317+
/**
318+
* Order method
319+
*
320+
* @return void
321+
* @access public
322+
*/
323+
public function testOrderASC() {
324+
$expected = array(19, 20);
325+
$result = $this->Post->find('all', array(
326+
'conditions' => array('title >' => 18),
327+
'fields' => array('_id', 'title'),
328+
'order' => array('title ASC')
329+
));
330+
$result = Set::extract($result, '/Post/title');
331+
$this->assertEqual($expected, $result);
332+
333+
$order = array(array('title' => 'ASC'));
334+
$this->assertEqual($order, $this->Post->lastQuery['order']);
335+
}
336+
337+
338+
/**
339+
* Order method with model alias
340+
*
341+
* @return void
342+
* @access public
343+
*/
344+
public function testOrderWithModelAlias() {
345+
$expected = array(20, 19);
346+
$result = $this->Post->find('all', array(
347+
'conditions' => array('title >' => 18),
348+
'fields' => array('_id', 'title'),
349+
'order' => array('Post.title DESC')
350+
));
351+
$result = Set::extract($result, '/Post/title');
352+
$this->assertEqual($expected, $result);
353+
}
354+
355+
356+
297357
/**
298358
* setupData method
299359
*
@@ -311,4 +371,4 @@ protected function _setupData() {
311371
$this->Post->save($saveData);
312372
}
313373
}
314-
}
374+
}

0 commit comments

Comments
 (0)