summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-09-14 04:03:39 +0100
committerJamie Matthews <[email protected]>2010-09-14 04:03:39 +0100
commit674d83234273ee9cf263c1278b96272a0f71e758 (patch)
treeedb6c1f317879e970036793a316c32b6cb171596
parent80c5136a539050896aa2901abcd91766d622f1e6 (diff)
Add code, tests and docs for inequality operators: where_gt, where_lt, where_gte, where_lte
-rw-r--r--README.markdown9
-rw-r--r--idiorm.php27
-rw-r--r--test/test_queries.php8
3 files changed, 44 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 1145928..b44bf45 100644
--- a/README.markdown
+++ b/README.markdown
@@ -105,6 +105,15 @@ To add a `WHERE ... LIKE` clause, use:
$people = ORM::for_table('person')->where_like('Name', '%fred%')->find_many();
+#### Less Than / Greater Than ####
+
+There are four methods available for inequalities:
+
+ * Less than: `$people = ORM::for_table('person')->where_lt('age', 10)->find_many();`
+ * Greater than: `$people = ORM::for_table('person')->where_gt('age', 5)->find_many();`
+ * Less than or equal: `$people = ORM::for_table('person')->where_lte('age', 10)->find_many();`
+ * Greater than or equal: `$people = ORM::for_table('person')->where_gte('age', 5)->find_many();`
+
#### Raw WHERE clauses ####
If you require a more complex query, you can use the `where_raw` method to specify the SQL fragment exactly. This method takes two arguments: the string to add to the query, and an array of parameters which will be bound to the string. The string should contain question marks to represent the values to be bound, and the parameter array should contain the values to be substituted into the string in the correct order.
diff --git a/idiorm.php b/idiorm.php
index 365fa00..89ddd1a 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -341,6 +341,33 @@
return $this->add_where($column_name, $value, 'LIKE');
}
+ /**
+ * Add a WHERE ... > clause to your query
+ */
+ public function where_gt($column_name, $value) {
+ return $this->add_where($column_name, $value, '>');
+ }
+
+ /**
+ * Add a WHERE ... < clause to your query
+ */
+ public function where_lt($column_name, $value) {
+ return $this->add_where($column_name, $value, '<');
+ }
+
+ /**
+ * Add a WHERE ... >= clause to your query
+ */
+ public function where_gte($column_name, $value) {
+ return $this->add_where($column_name, $value, '>=');
+ }
+
+ /**
+ * Add a WHERE ... <= clause to your query
+ */
+ public function where_lte($column_name, $value) {
+ return $this->add_where($column_name, $value, '<=');
+ }
/**
* Add a raw WHERE clause to the query. The clause should
diff --git a/test/test_queries.php b/test/test_queries.php
index 7670df6..bc716d1 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -62,6 +62,14 @@
$expected = 'SELECT * FROM widget WHERE name = "Fred" ORDER BY name ASC LIMIT 5 OFFSET 5';
Tester::check_equal("Complex query", $expected);
+ ORM::for_table('widget')->where_lt('age', 10)->where_gt('age', 5)->find_many();
+ $expected = 'SELECT * FROM widget WHERE age < "10" AND age > "5"';
+ Tester::check_equal("Less than and greater than", $expected);
+
+ ORM::for_table('widget')->where_lte('age', 10)->where_gte('age', 5)->find_many();
+ $expected = 'SELECT * FROM widget WHERE age <= "10" AND age >= "5"';
+ Tester::check_equal("Less than or equal and greater than or equal", $expected);
+
ORM::for_table('widget')->where_raw('name = ? AND (age = ? OR age = ?)', array('Fred', 5, 10))->find_many();
$expected = 'SELECT * FROM widget WHERE name = "Fred" AND (age = "5" OR age = "10")';
Tester::check_equal("Raw WHERE clause", $expected);