summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown8
-rw-r--r--idiorm.php8
-rw-r--r--test/test_queries.php4
3 files changed, 18 insertions, 2 deletions
diff --git a/README.markdown b/README.markdown
index 2e6e6b5..cbc942b 100644
--- a/README.markdown
+++ b/README.markdown
@@ -202,12 +202,16 @@ The `limit` and `offset` methods map pretty closely to their SQL equivalents.
##### Ordering #####
-*Note that this method **does not** escape its query parameter and so this should **not** be passed directly from user input.*
+*Note that these methods **do not** escape their query parameters and so these should **not** be passed directly from user input.*
-Two methods are provided to add `ORDER BY` clauses to your query. These are `order_by_desc` and `order_by_asc`, each of which takes a column name to sort by.
+Two methods are provided to add `ORDER BY` clauses to your query. These are `order_by_desc` and `order_by_asc`, each of which takes a column name to sort by. The column names will be quoted.
$people = ORM::for_table('person')->order_by_asc('gender')->order_by_desc('name')->find_many();
+If you want to order by something other than a column name, then use the `order_raw` method to add a raw `ORDER BY` clause.
+
+ $people = ORM::for_table('person')->order_raw('SOUNDEX(`name`)')->find_many();
+
#### Grouping ####
*Note that this method **does not** escape it query parameter and so this should **not** by passed directly from user input.*
diff --git a/idiorm.php b/idiorm.php
index 4d454cb..b4e4f7d 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -749,6 +749,14 @@
}
/**
+ * Add a raw ORDER BY clause
+ */
+ public function order_raw($clause) {
+ $this->_order_by[] = $clause;
+ return $this;
+ }
+
+ /**
* Add a column to the list of columns to GROUP BY
*/
public function group_by($column_name) {
diff --git a/test/test_queries.php b/test/test_queries.php
index 329ebc9..b4f3341 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -80,6 +80,10 @@
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1";
Tester::check_equal("ORDER BY ASC", $expected);
+ ORM::for_table('widget')->order_raw('SOUNDEX(`name`)')->find_one();
+ $expected = "SELECT * FROM `widget` ORDER BY SOUNDEX(`name`) LIMIT 1";
+ Tester::check_equal("Raw ORDER BY", $expected);
+
ORM::for_table('widget')->order_by_asc('name')->order_by_desc('age')->find_one();
$expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1";
Tester::check_equal("Multiple ORDER BY", $expected);