Article

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');
    }
 
    $query->limit(0);
    
    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 . '\'';
        }
 
        $iQC++;
   }
   for($iQC;$iQC < count($queryStringParts);$iQC++) {
       $queryString .= $queryStringParts[$iQC];
   }
 
    echo $queryString;
}

I hope that it proves useful.

me.jpeg

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!

Advertisement

Digital Ocean

Chris's Reading List

John Doherty

Does A Startup Really Need The .Com Of Their Brand Name?

Recently Paul Graham, a VC and entrepreneur whom I very much respect, wrote a post called Change Your Name in which he argued that a startup should seriously think about changing their name if they are not able to get the “.com” … Read more

SitePoint

SitePoint

Look, Ma! No NodeJS! - a PHP front end setup without Node

Bruno Skvorc combines BowerPHP, Mini-Asset and Robo.li into a PHP front end asset building pipeline that aims to completely avoid having to deal with NodeJS Read more

Moz

Moz

Why No One Pays Attention to Your Marketing - Whiteboard Friday

Brand fatigue is a real threat to your marketing strategy. In today's Whiteboard Friday, Rand highlights some common causes of brand fatigue and how to combat it. Read more

UserTesting Blog

3 Most Common Mistakes Marketers Make When Running User Tests

As marketers, we rely on data to guide our decisions. We run A/B tests. We use robust analytics tools. We find out where customers are converting, and where they’re not. We know exactly what our customers are doing. Read more

Medium

Medium

How a single change boosted organic traffic by 40%

The SEO world can sometimes be a confusing maze of contradictions. Read more

verraes.net

verraes.net

Sensible Interfaces

How did we get from 'Program to an interface, not to an implementation' to 'Just slap an interface on there, it's the fashionable thing to do'? Read more