Friday, December 5, 2008

How to make native SQL queries in Doctrine ORM

Sometimes you have situation which require writing of native SQL query in project fully based on Doctrine ORM. Doing DQL queries is very easy, but simple SQL queries maybe difficult to execute because you have to reuse database connection opened inside Doctrine.

Example given below is easy to understand and use in your project(s). Enjoy!
// get Doctrine_Connection object
$con = Doctrine_Manager::getInstance()->connection();
// execute SQL query, receive Doctrine_Connection_Statement
$st = $con->execute("...............");
// fetch query result
$result = $st->fetchAll();