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

Chris Shennan

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!

Chris's Reading List

Moz

Moz

The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

A fantastic piece of content can lift your traffic curves to a "new normal." In today's Whiteboard Friday, Rand talks about exactly how that happens, and how you can create content that has that effect. Read more

Loige

Loige

6 Rules of thumb to build blazing fast web applications (server side)

6 Rules of thumb to build blazing fast web applications (server side)

<p>In this post are described 6 extremely important rules you should take under consideration when writing your next web application</p> Read more

SitePoint

SitePoint

Console Wars - PHP CLI Libraries

Console Wars - PHP CLI Libraries

Nicola Pietroluongo compares three popular PHP console libraries, building the same functionality in each. Wondering which one is right for you? Find out! Read more

TechCrunch

TechCrunch

Servers.com Brings Its Bare-Metal Servers To The US

Servers.com Brings Its Bare-Metal Servers To The US

Servers.com, a hosting company with a focus on dedicated bare-metal servers that launched in Europe in 2005, today announced the opening of its first U.S... Read more

Blonde

Blonde

PHP in a JavaScript world

PHP in a JavaScript world

What is the fate of PHP in a JavaScript-dominated world? Will the two continue coexisting? Stephen Heron, PHP Developer at Blonde, shares his thoughts. Read more

Advertisement

Digital Ocean