summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-11-03 23:09:28 +0000
committerJamie Matthews <[email protected]>2010-11-03 23:09:28 +0000
commit9a639619d80c9b0d640a3a79fd8779a375c5b28b (patch)
treebb35f23ebe6d5d10112f8edecd502b718286a754
parentda6d31366f809720d0cd411ddc02249c518ce9f5 (diff)
Add where_null and where_not_null methods. Thanks for the suggestion, artcijsg
-rw-r--r--README.markdown4
-rw-r--r--idiorm.php18
-rw-r--r--test/test_queries.php8
3 files changed, 29 insertions, 1 deletions
diff --git a/README.markdown b/README.markdown
index c520e0b..75d9a28 100644
--- a/README.markdown
+++ b/README.markdown
@@ -141,6 +141,10 @@ Both methods accept two arguments. The first is the column name to compare again
$people = ORM::for_table('person')->where_in('name', array('Fred', 'Joe', 'John'))->find_many();
+##### Working with `NULL` values: `where_null` and `where_not_null` #####
+
+To add a `WHERE column IS NULL` or `WHERE column IS NOT NULL` clause, use the `where_null` and `where_not_null` methods respectively. Both methods accept a single parameter: the column name to test.
+
##### Raw WHERE clauses #####
If you require a more complex query, you can use the `where_raw` method to specify the SQL fragment for the WHERE clause exactly. This method takes two arguments: the string to add to the query, and an (optional) array of parameters which will be bound to the string. If parameters are supplied, the string should contain question mark characters (`?`) 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 1fbd54c..98b14b5 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -534,7 +534,7 @@
/**
* Internal method to add a WHERE condition to the query
*/
- protected function _add_where($fragment, $values) {
+ protected function _add_where($fragment, $values=array()) {
if (!is_array($values)) {
$values = array($values);
}
@@ -657,6 +657,22 @@
}
/**
+ * Add a WHERE column IS NULL clause to your query
+ */
+ public function where_null($column_name) {
+ $column_name = $this->_quote_identifier($column_name);
+ return $this->_add_where("{$column_name} IS NULL");
+ }
+
+ /**
+ * Add a WHERE column IS NOT NULL clause to your query
+ */
+ public function where_not_null($column_name) {
+ $column_name = $this->_quote_identifier($column_name);
+ return $this->_add_where("{$column_name} IS NOT NULL");
+ }
+
+ /**
* Add a raw WHERE clause to the query. The clause should
* contain question mark placeholders, which will be bound
* to the parameters supplied in the second argument.
diff --git a/test/test_queries.php b/test/test_queries.php
index 879a086..68223c6 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -96,6 +96,14 @@
$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_null('name')->find_many();
+ $expected = "SELECT * FROM `widget` WHERE `name` IS NULL";
+ Tester::check_equal("where_null method", $expected);
+
+ ORM::for_table('widget')->where_not_null('name')->find_many();
+ $expected = "SELECT * FROM `widget` WHERE `name` IS NOT NULL";
+ Tester::check_equal("where_not_null method", $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);