Raw SQL from Doctrine Query Object - Revised

A few months ago I posted an article taking about how to get the raw SQL from a Doctrine Query Object but with the release of Symfony 1.3 and Symfony 1.4 it would appear that the code no longer works.  As a result I've updated the code to work with Symfony 1.2 - 1.4 and you can find the updated source below:-

function get_raw_sql($query) {
    if(!($query instanceof Doctrine_Query)) {
        throw new sfException('Not an instanse of a Doctrine Query');
    if(is_callable(array($query, 'buildSqlQuery'))) {
        $queryString = $query->buildSqlQuery();
        $query_params = $query->getParams();
        $params = $query_params['where'];
    } else {
        $queryString = $query->getSql();
        $params = $query->getParams();
    $queryStringParts = split('\?', $queryString);
    $iQC = 0;
    $queryString = "";
    foreach($params as $param) {
        if(is_numeric($param)) {
            $queryString .= $queryStringParts[$iQC] . $param;
        } elseif(is_bool($param)) {
            $queryString .= $queryStringParts[$iQC] . $param*1;
        } else {
            $queryString .= $queryStringParts[$iQC] . '\'' . $param . '\'';
   for($iQC;$iQC < count($queryStringParts);$iQC++) {
       $queryString .= $queryStringParts[$iQC];
    echo $queryString;

I hope that it proves useful.


I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for the The Drum in Glasgow, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!


Digital Ocean

Chris's Reading List



Prefetching, preloading, prebrowsing | CSS-Tricks

When we talk about front end performance we think things like concatenation, minification, caching, or gzipping assets on the server so that the page loads Read more



Legendary Productivity And The Fear Of Modern Programming

JavaScript master Douglas Crockford once said that software is the most complex thing that humans have ever created. It’s made up of intricate bits and.. Read more

Confession: I was a teenage computer virus writer

Did your PC crash a lot in the 90s? Yes, sorry about that Read more

UserTesting Blog

UserTesting Blog

To Design an Awesome Product, Let Go of Being Right

I recently had a conversation with a designer who was working on the early stages of a new product. Naturally, I recommended getting user feedback early and often throughout the design process. And he said… “But what if what your … Read More Read more

Acer's coolest new PC lets you build a computer like Lego bricks

Building your own computer is starting to come back in style thanks to initiatives like Phonebloks and Google's Project Ara, and now Acer is bringing that idea to the desktop. It's introducing a... Read more

Google will demote mobile sites that use terrible app install ads

There are lots of problems with the mobile web, but one of the most annoying is clicking through a search result and having your entire screen taken over by an app install ad. You search in vain... Read more