diff options
-rw-r--r-- | idiorm.php | 18 | ||||
-rwxr-xr-x | test/test_queries.php | 4 |
2 files changed, 22 insertions, 0 deletions
@@ -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. * diff --git a/test/test_queries.php b/test/test_queries.php index 984b407..8cc26d6 100755 --- a/test/test_queries.php +++ b/test/test_queries.php @@ -295,6 +295,10 @@ $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(); |