Skip to content

Commit

Permalink
Add raw_execute() to ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
tag authored and treffynnon committed Nov 26, 2012
1 parent e2cb224 commit f6d7861
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,24 @@ public static function get_db() {
return self::$_db;
}

/**
* 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.
Expand Down
4 changes: 4 additions & 0 deletions test/test_queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f6d7861

Please sign in to comment.