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!

Want more like this?

Keep up-to-date with latest news from Inside the Agency using any of the following services

Recent articles

The Iron Triangle - How does motivation emphasis create an agency environment?

I remember when I first heard about the Iron Triangle. It sounded sinister, Evil Empire like, and not really something I could embrace in my fluffy idealistic agency brain. Just to be clear in this article I am talking about the Project Management version, not the US political system version… which is sinister and is just like the Evil Empire.


Back then I was speaking to my then boss, a man who I credit with opening my eyes to a great deal to how an agency works and, more importantly, how it should work. He was explaining in typically simple eloquence, how he wanted us to set expectation with clients. 

Read more

The Adblock Wars - Stop blaming the victim!

Much to the dismay of the advertising and publishing industry, Adblock is indeed here to stay. But, what is the industry doing about it? For the most part, pointing the finger and accusing the end user of underhanded tactics. To that I say: Pot, meet Kettle.


Over the last few months I have noticed an increasing number of articles surround Adblock usage, most of which are coming from advertisers and publishers perspective.  Within these articles the underlying theme is that Adblock is evil and people using it are:

Read more

Content is a very lonely king (and he needs some pants)

I was once told that If you put a £5 note for sale in the window, you can sell it for £5 or maybe even more, but if you put it way at the back of the shop, hidden away and try to sell it for 50p, it still won't sell.


Lesson:  No matter how good the product is, if it's not where your customers can see it then it's worthless. 

Read more


Digital Ocean

Chris's Reading List


Self-promotion is a skill, but is that the skill you're looking for? - JAXenter

Do you want to hire somebody that's good at self-promotion, or are you looking for a certain skill-set? Perhaps you need to shape your job spec differently. Read more



When Crowdfunding Fails The Backers Are Left With No Way Out

A few years ago an enterprising programmer dug through Kickstarter's data to figure out how many failed projects there really were. Thanks to a trick in the.. Read more

Business Insider

A programmer wrote scripts to secretly automate a lot of his job — and email his wife and make a latte

There's a hilarious project that's gone viral on GitHub that consists of a bunch of software scripts that automate all sorts of crazy things. Read more

The Bests

The Best-Looking Video Game Consoles Of All Time

You silly fanboys. All these years arguing over things you thought were important, like specs, and libraries, and exclusives, and controllers, wasted. Not once have you considered the most crucial thing about a video game console: how good it looks. Read more

The Founder Institute

The Founder Institute How to Build an MVP as a Non-Technical Founder

The Founder Institute hosts webinars every month so that we can help entrepreneurs on any stage succeed. These webinars discuss a wide range of topics, bestowing global entrepreneurs with invaluabl... Read more



Google’s New “About Me” Page Lets You Control What Personal Info Others Can See

Worried that Google has too much of your personal data, thanks to the way it has pried into your life over the years as you steadily adopted more of its.. Read more