Using Raw SQL with Doctrine Using Raw SQL with Doctrine sql sql

Using Raw SQL with Doctrine


$q = Doctrine_Manager::getInstance()->getCurrentConnection();$result = $q->execute(" -- RAW SQL HERE -- ");

See the Doctrine API documentation for different execution methods.


Yes. You can get a database handle from Doctrine using the following code:

$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();

and then execute your SQL as follows:

$query = "SELECT * FROM table WHERE param1 = :param1 AND param2 = :param2";$stmt = $pdo->prepare($query);$params = array(  "param1"  => "value1",  "param2"  => "value2");$stmt->execute($params);$results = $stmt->fetchAll();  

You can use bound variables as in the above example.

Note that Doctrine won't automatically hydrate your results nicely into record objects etc, so you'll need to deal with the results being returned as an array, consisting of one array per row returned (key-value as column-value).


I'm not sure what do you mean saying raw SQL, but you coud execute traditional SQL queries this way:

... // $this->_displayPortabilityWarning();$conn = Doctrine_Manager::connection();$pdo = $conn->execute($sql);$pdo->setFetchMode(Doctrine_Core::FETCH_ASSOC);$result = $pdo->fetchAll();...

The following method is not necsessary, but it shows a good practice.

protected function _displayPortabilityWarning($engine = 'pgsql'){     $conn = Doctrine_Manager::connection();     $driver = $conn->getDriverName();     if (strtolower($engine) != strtolower($driver)) {        trigger_error('Here we have possible database portability issue. This code was tested on ' . $engine . ' but you are trying to run it on ' . $driver, E_USER_NOTICE);     }}