summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown16
-rw-r--r--idiorm.php36
-rwxr-xr-x[-rw-r--r--]test/test_queries.php14
3 files changed, 63 insertions, 3 deletions
diff --git a/README.markdown b/README.markdown
index dcad3a4..4078f6a 100644
--- a/README.markdown
+++ b/README.markdown
@@ -25,6 +25,12 @@ Features
Changelog
---------
+#### 1.3.0 - release XXXX-XX-XX
+
+* Add in raw_execute - closes issue #40 [[tag](https://github.com/tag)]
+* Add query logging to `delete_many` [[tag](https://github.com/tag)]
+* Add `is_new` method - closes issue #85
+
#### 1.2.3 - release 2012-11-28
* Fix issue #78 - remove use of PHP 5.3 static call
@@ -401,6 +407,8 @@ The other functions (`AVG`, `MAX` and `SUM`) work in exactly the same manner. Su
#### Raw queries ####
+##### Return Idiorm instances #####
+
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();
@@ -409,6 +417,12 @@ The ORM class instance(s) returned will contain data for all the columns returne
Note that using `raw_query` is advanced and possibly dangerous, and Idiorm does not make any attempt to protect you from making errors when using this method. If you find yourself calling `raw_query` often, you may have misunderstood the purpose of using an ORM, or your application may be too complex for Idiorm. Consider using a more full-featured database abstraction system.
+##### Direct PDO access raw queries #####
+
+It is possible to skip Idiorm's query building system altogether by using `raw_execute`. You will not get an Idiorm instance as a result and it is also potentially dangerous just like the method above.
+
+ $people = ORM::raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table` WHERE id = ?', array(10));
+
### Getting data from objects ###
Once you've got a set of records (objects) back from a query, you can access properties on those objects (the values stored in the columns in its corresponding table) in two ways: by using the `get` method, or simply by accessing the property on the object directly:
@@ -479,6 +493,8 @@ To add a new record, you need to first create an "empty" object instance. You th
After the object has been saved, you can call its `id()` method to find the autogenerated primary key value that the database assigned to it.
+To determine if the instance you are operating on has been obtained by calling `create()` or whether it was via a query on the database you can call `is_new()` on it to get a boolean response.
+
#### Properties containing expressions ####
It is possible to set properties on the model that contain database expressions using the `set_expr` method.
diff --git a/idiorm.php b/idiorm.php
index 9d1f8c4..23e297c 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -256,6 +256,24 @@
}
/**
+ * Executes a raw query as a wrapper for PDOStatement::execute.
+ * Useful for queries that can't be accomplished through Idiorm,
+ * particularly those using engine-specific features.
+ * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10')
+ * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`')
+ * @param string $query The raw SQL query
+ * @param array $parameters Optional bound parameters
+ * @return bool Success
+ */
+ public static function raw_execute($query, $parameters = array()) {
+ self::_setup_db();
+
+ self::_log_query($query, $parameters);
+ $statement = self::$_db->prepare($query);
+ return $statement->execute($parameters);
+ }
+
+ /**
* Add a query to the internal query log. Only works if the
* 'logging' config option is set to true.
*
@@ -1226,6 +1244,15 @@
$this->_set_orm_property($key, $value);
}
+ /**
+ * Set a property to a particular value on this object.
+ * To set multiple properties at once, pass an associative array
+ * as the first parameter and leave out the second parameter.
+ * Flags the properties as 'dirty' so they will be saved to the
+ * database when save() is called.
+ * @param string|array $key
+ * @param string|null $value
+ */
public function set_expr($key, $value = null) {
$this->_set_orm_property($key, $value, true);
}
@@ -1260,6 +1287,14 @@
}
/**
+ * Check whether the model was the result of a call to create() or not
+ * @return bool
+ */
+ public function is_new() {
+ return $this->_is_new;
+ }
+
+ /**
* Save any fields which have been modified on this object
* to the database.
*/
@@ -1360,6 +1395,7 @@
$this->_quote_identifier($this->_table_name),
$this->_build_where(),
));
+ self::_log_query($query, $this->_values);
$statement = self::$_db->prepare($query);
return $statement->execute($this->_values);
}
diff --git a/test/test_queries.php b/test/test_queries.php
index bcd4c7e..8cc26d6 100644..100755
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -291,20 +291,28 @@
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
Tester::check_equal("Delete data", $expected);
+ $widget = ORM::for_table('widget')->where_equal('age', 10)->delete_many();
+ $expected = "DELETE FROM `widget` WHERE `age` = '10'";
+ Tester::check_equal("Delete many", $expected);
+
+ ORM::raw_execute("INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES (?, ?)", array(1, 'Tolstoy'));
+ $expected = "INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES ('1', 'Tolstoy')";
+ Tester::check_equal("Raw execute", $expected); // A bit of a silly test, as query is passed through
+
// Regression tests
$widget = ORM::for_table('widget')->select('widget.*')->find_one();
$expected = "SELECT `widget`.* FROM `widget` LIMIT 1";
Tester::check_equal("Issue #12 - incorrect quoting of column wildcard", $expected);
-
+
$widget = ORM::for_table('widget')->where_raw('username LIKE "ben%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE username LIKE "ben%"';
Tester::check_equal('Issue #57 - _log_query method raises a warning when query contains "%"', $expected);
-
+
$widget = ORM::for_table('widget')->where_raw('comments LIKE "has been released?%"')->find_many();
$expected = 'SELECT * FROM `widget` WHERE comments LIKE "has been released?%"';
Tester::check_equal('Issue #57 - _log_query method raises a warning when query contains "?"', $expected);
-
+
// Tests that alter Idiorm's config are done last
ORM::configure('id_column', 'primary_key');