summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-02-13 13:06:06 +0000
committerJamie Matthews <[email protected]>2010-02-13 13:06:06 +0000
commit78cbb3e73ca7c615cde82bf440128e6e71c8bc1a (patch)
tree9cbc4528c097b4248bc13ef1298ddddd148fea74
parentd5854cbe3dc605b09dd9dab094a9e4a9d0054f03 (diff)
Added where_like method, documentation and tests. Changing API to use alternate method calls instead of class constants to specify WHERE operator.
-rw-r--r--README.markdown10
-rw-r--r--idiorm.php17
-rw-r--r--test/test_queries.php4
3 files changed, 24 insertions, 7 deletions
diff --git a/README.markdown b/README.markdown
index 2b90921..f537a26 100644
--- a/README.markdown
+++ b/README.markdown
@@ -84,9 +84,15 @@ To find all records where the `gender` is `female`:
#### WHERE clauses ####
-The `where` method on the ORM class adds a single `WHERE` clause to your query. The method may be called (chained) multiple times to add more than one WHERE clause. All the WHERE clauses will be ANDed together when the query is run. Support for ORing WHERE clauses is not currently present; if a query requires an OR clause you should use the `where_raw` or `raw_select` methods (see below). [TODO]
+The `where` method on the ORM class adds a single `WHERE` clause to your query. The method may be called (chained) multiple times to add more than one WHERE clause. All the WHERE clauses will be ANDed together when the query is run. Support for ORing WHERE clauses is not currently present; if a query requires an OR clause you should use the `where_raw` or `raw_select` methods (see below).
-By default, calling `where` with two parameters (the column name and the value) will combine them using an equals operator (`=`). For example, calling `where('name', 'Fred')` will result in the clause `WHERE name = "Fred"`. However, the `where` method takes an optional third parameter which specifies the type of operator to use. Constants for each operator are provided on the ORM class. Currently, the supported operators are: `ORM::EQUALS` and `ORM::LIKE`.
+By default, calling `where` with two parameters (the column name and the value) will combine them using an equals operator (`=`). For example, calling `where('name', 'Fred')` will result in the clause `WHERE name = "Fred"`. To use other types of operator, you can use one of the alternate `where_*` methods below.
+
+##### LIKE #####
+
+To add a `WHERE ... LIKE` clause, use:
+
+ ORM::for_table('person')->where_like('Name', '%fred%')->find_many();
#### Raw WHERE clauses ####
diff --git a/idiorm.php b/idiorm.php
index 96b9b1d..1276f8e 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -45,14 +45,9 @@
// ----------------------- //
// Select WHERE operators
- // These are "public" API and should
- // be used (if needed) as the third
- // argument to the where() method.
const EQUALS = '=';
const LIKE = 'LIKE';
- // The rest are "private" API.
-
// Find types
const FIND_ONE = 0;
const FIND_MANY = 1;
@@ -305,6 +300,10 @@
* parameter to this method may be used to indicate other
* operators such as LIKE. Class constants should be used to
* provide this operator.
+ *
+ * Note: the $operator argument is now really private API. The
+ * where_* methods below should be used instead to add different
+ * types of WHERE clause.
*/
public function where($column_name, $value, $operator=self::EQUALS) {
$this->where[] = array(
@@ -316,6 +315,14 @@
}
/**
+ * Add a WHERE ... LIKE clause to your query.
+ */
+ public function where_like($column_name, $value) {
+ return $this->where($column_name, $value, self::LIKE);
+ }
+
+
+ /**
* 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 144be56..ac90c5e 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -30,6 +30,10 @@
$expected = 'SELECT * FROM widget WHERE name = "Fred" AND age = "10"';
Tester::check_equal("Multiple WHERE clauses", $expected);
+ ORM::for_table('widget')->where_like('name', '%Fred%')->find_one();
+ $expected = 'SELECT * FROM widget WHERE name LIKE "%Fred%"';
+ Tester::check_equal("where_like method", $expected);
+
ORM::for_table('widget')->limit(5)->find_one();
$expected = 'SELECT * FROM widget LIMIT "5"';
Tester::check_equal("LIMIT clause", $expected);