summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-10-07 01:31:05 +0100
committerJamie Matthews <[email protected]>2010-10-07 01:31:05 +0100
commit69bf5735df7360264ad1763fc5bc303411bd8f6c (patch)
treeb4ffbbc8a36380602bbfa38418c85491515dedc6
parent6c9356097dd42cdbc85481a40b7ecd2ce4d3f146 (diff)
Simplify where_raw method. This method can now be used in method chains.
-rw-r--r--README.markdown14
-rw-r--r--idiorm.php21
-rw-r--r--test/test_queries.php5
3 files changed, 16 insertions, 24 deletions
diff --git a/README.markdown b/README.markdown
index 6db94ed..4bbd325 100644
--- a/README.markdown
+++ b/README.markdown
@@ -140,13 +140,19 @@ Both methods accept two arguments. The first is the column name to compare again
##### 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.
+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 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.
-This method may be chained with other methods such as `offset`, `limit` and `order_by_(*)`, but it may NOT be chained with other calls to `where`. If other `where` calls are present in the method chain, they will simply be ignored in the resulting query.
+This method may be used in a method chain alongside other `where_*` methods as well as methods such as `offset`, `limit` and `order_by_(*)`. The contents of the string you supply will be connected with preceding and following WHERE clauses with AND.
- $people = ORM::for_table('person')->where_raw('name = ? AND (age = ? OR age = ?)', array('Fred', 5, 10))->order_by_asc('name')->find_many();
+ $people = ORM::for_table('person')
+ ->where_raw('name = ? AND (age = ? OR age = ?)', array('Fred', 5, 10))
+ ->where('size', 'LARGE')
+ ->order_by_asc('name')
+ ->find_many();
-Note that this method only supports "question mark placeholder" syntax, and NOT "named placeholder" syntax.
+Note that this method only supports "question mark placeholder" syntax, and NOT "named placeholder" syntax. This is because PDO does not allow queries that contain a mixture of placeholder types. Also, you should ensure that the number of question mark placeholders in the string exactly matches the number of elements in the array.
+
+If you require yet more flexibility, you can manually specify the entire query. See *Raw queries* below.
##### Limits and offsets #####
diff --git a/idiorm.php b/idiorm.php
index 62de812..65e9189 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -104,15 +104,6 @@
// Array of WHERE clauses
protected $_where_conditions = array();
- // Is the WHERE clause raw?
- protected $_where_is_raw = false;
-
- // Raw WHERE clause
- protected $_raw_where_clause = '';
-
- // Raw WHERE parameters
- protected $_raw_where_parameters = array();
-
// LIMIT
protected $_limit = null;
@@ -425,10 +416,7 @@
* to the parameters supplied in the second argument.
*/
public function where_raw($clause, $parameters) {
- $this->_where_is_raw = true;
- $this->_raw_where_clause = $clause;
- $this->_raw_where_parameters = $parameters;
- return $this;
+ return $this->_add_where($clause, $parameters);
}
/**
@@ -511,13 +499,6 @@
* Build the WHERE clause(s)
*/
protected function _build_where() {
- // If the WHERE clause is raw, just set $this->_values to
- // the raw parameters and return the raw clause.
- if ($this->_where_is_raw) {
- $this->_values = array_merge($this->_values, $this->_raw_where_parameters);
- return "WHERE " . $this->_raw_where_clause;
- }
-
// If there are no WHERE clauses, return empty string
if (count($this->_where_conditions) === 0) {
return '';
diff --git a/test/test_queries.php b/test/test_queries.php
index aadfe46..013b897 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -86,6 +86,11 @@
$expected = 'SELECT * FROM `widget` WHERE `name` = "Fred" AND (`age` = "5" OR `age` = "10")';
Tester::check_equal("Raw WHERE clause", $expected);
+ ORM::for_table('widget')->where('age', 18)->where_raw('(`name` = ? OR `name` = ?)', array('Fred', 'Bob'))->where('size', 'large')->find_many();
+ $expected = 'SELECT * FROM `widget` WHERE `age` = "18" AND (`name` = "Fred" OR `name` = "Bob") AND `size` = "large"';
+ Tester::check_equal("Raw WHERE clause in method chain", $expected);
+
+
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = ? AND `age` = ?', array('Fred', 5))->find_many();
$expected = 'SELECT `w`.* FROM `widget` w WHERE `name` = "Fred" AND `age` = "5"';
Tester::check_equal("Raw query", $expected);