diff --git a/lib/Model.php b/lib/Model.php index b8492c810..1f84102cc 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -1478,6 +1478,18 @@ public static function find_by_sql($sql, $values=null) return static::table()->find_by_sql($sql, $values, true); } + /** + * Helper method to run arbitrary queries against the model's database connection. + * + * @param string $sql SQL to execute + * @param array $values Bind values, if any, for the query + * @return object A PDOStatement object + */ + public static function query($sql, $values=null) + { + return static::connection()->query($sql, $values); + } + /** * Determines if the specified array is a valid ActiveRecord options array. * diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 44af28ddf..085bfef04 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -469,5 +469,14 @@ public function test_id_setter_works_with_table_without_pk_named_attribute() $author = new Author(array('id' => 123)); $this->assert_equals(123,$author->author_id); } + + public function test_query() + { + $row = Author::query('SELECT COUNT(*) AS n FROM authors',null)->fetch(); + $this->assert_true($row['n'] > 1); + + $row = Author::query('SELECT COUNT(*) AS n FROM authors WHERE name=?',array('Tito'))->fetch(); + $this->assert_equals(array('n' => 1), $row); + } }; ?>