Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add SQL style of Model ordering syntax

using SqlCompatible behavior,
you can use
$order = 'History.created DESC';

close #20
  • Loading branch information...
commit f0d296b54d4b085ffca7b6ba61cd3f6051ce8b11 1 parent 739b004
@ichikaway authored
View
29 models/behaviors/sql_compatible.php
@@ -109,10 +109,13 @@ public function afterFind(&$Model, $results, $primary) {
* @access public
*/
public function beforeFind(&$Model, $query) {
+ if (is_array($query['order'])) {
+ $this->_translateOrders($Model, $query['order']);
+ }
if (is_array($query['conditions']) && $this->_translateConditions($Model, $query['conditions'])) {
return $query;
}
- return true;
+ return $query;
}
/**
@@ -132,6 +135,28 @@ public function convertDates(&$results) {
}
}
+
+/**
+ * translateOrders method
+ * change order syntax from SQL style to Mongo style
+ *
+ * @param mixed $Model
+ * @param mixed $orders
+ * @return void
+ * @access protected
+ */
+ protected function _translateOrders(&$Model, &$orders) {
+ if(!empty($orders[0])) {
+ foreach($orders[0] as $key => $val) {
+ if(preg_match('/^(.+) (ASC|DESC)$/i', $val, $match)) {
+ $orders[0][$match[1]] = $match[2];
+ unset($orders[0][$key]);
+ }
+ }
+ }
+ }
+
+
/**
* translateConditions method
*
@@ -267,4 +292,4 @@ protected function _translateOperator($Model, $operator) {
}
return '';
}
-}
+}
View
62 tests/cases/behaviors/sql_compatible.test.php
@@ -295,6 +295,66 @@ public function testFindConditionIn() {
/**
+ * Order method
+ *
+ * @return void
+ * @access public
+ */
+ public function testOrderDESC() {
+ $expected = array(20, 19);
+ $result = $this->Post->find('all', array(
+ 'conditions' => array('title >' => 18),
+ 'fields' => array('_id', 'title'),
+ 'order' => array('title DESC')
+ ));
+ $result = Set::extract($result, '/Post/title');
+ $this->assertEqual($expected, $result);
+
+ $order = array(array('title' => 'DESC'));
+ $this->assertEqual($order, $this->Post->lastQuery['order']);
+ }
+
+/**
+ * Order method
+ *
+ * @return void
+ * @access public
+ */
+ public function testOrderASC() {
+ $expected = array(19, 20);
+ $result = $this->Post->find('all', array(
+ 'conditions' => array('title >' => 18),
+ 'fields' => array('_id', 'title'),
+ 'order' => array('title ASC')
+ ));
+ $result = Set::extract($result, '/Post/title');
+ $this->assertEqual($expected, $result);
+
+ $order = array(array('title' => 'ASC'));
+ $this->assertEqual($order, $this->Post->lastQuery['order']);
+ }
+
+
+/**
+ * Order method with model alias
+ *
+ * @return void
+ * @access public
+ */
+ public function testOrderWithModelAlias() {
+ $expected = array(20, 19);
+ $result = $this->Post->find('all', array(
+ 'conditions' => array('title >' => 18),
+ 'fields' => array('_id', 'title'),
+ 'order' => array('Post.title DESC')
+ ));
+ $result = Set::extract($result, '/Post/title');
+ $this->assertEqual($expected, $result);
+ }
+
+
+
+/**
* setupData method
*
* @return void
@@ -311,4 +371,4 @@ protected function _setupData() {
$this->Post->save($saveData);
}
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.