summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown2
-rw-r--r--idiorm.php12
-rw-r--r--test/test_queries.php6
3 files changed, 12 insertions, 8 deletions
diff --git a/README.markdown b/README.markdown
index 40d0067..9ebb1b9 100644
--- a/README.markdown
+++ b/README.markdown
@@ -301,7 +301,7 @@ The `join` methods also take an optional third parameter, which is an `alias` fo
#### Raw queries ####
-If you need to perform more complex queries, you can completely specify the query to execute by using the `raw_query` method. This method takes a string and an array of parameters. The string should contain placeholders, either in question mark or named placeholder syntax, which will be used to bind the parameters to the query.
+If you need to perform more complex queries, you can completely specify the query to execute by using the `raw_query` method. This method takes a string and optionally an array of parameters. The string can contain placeholders, either in question mark or named placeholder syntax, which will be used to bind the parameters to the query.
$people = ORM::for_table('person')->raw_query('SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'janitor')->find_many();
diff --git a/idiorm.php b/idiorm.php
index 3dd49b0..2c75776 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -411,13 +411,13 @@
}
/**
- * Perform a raw query. The query should contain placeholders,
- * in either named or question mark style, and the parameters
- * should be an array of values which will be bound to the
- * placeholders in the query. If this method is called, all
- * other query building methods will be ignored.
+ * Perform a raw query. The query can contain placeholders in
+ * either named or question mark style. If placeholders are
+ * used, the parameters should be an array of values which will
+ * be bound to the placeholders in the query. If this method
+ * is called, all other query building methods will be ignored.
*/
- public function raw_query($query, $parameters) {
+ public function raw_query($query, $parameters = array()) {
$this->_is_raw_query = true;
$this->_raw_query = $query;
$this->_raw_parameters = $parameters;
diff --git a/test/test_queries.php b/test/test_queries.php
index 35896e3..217b7fd 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -128,9 +128,13 @@
$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')->find_many();
+ $expected = "SELECT `w`.* FROM `widget` w";
+ Tester::check_equal("Raw query", $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);
+ Tester::check_equal("Raw query with parameters", $expected);
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";