Take the 2-minute tour ×

We're using Doctrine, a PHP ORM. I am creating a query like this:

$q = Doctrine_Query::create()->select('id')->from('MyTable');

and then in the function I'm adding in various where clauses and things as appropriate, like this

$q->where('normalisedname = ? OR name = ?', array($string, $originalString));

Later on, before execute()-ing that query object, I want to print out the raw SQL in order to examine it, and do this:


However that only prints out the prepared statement, not the full query. I want to see what it is sending to the MySQL, but instead it is printing out a prepared statement, including ?'s. Is there some way to see the 'full' query?

share|improve this question
add comment

8 Answers

up vote 17 down vote accepted

Doctrine is not sending a "real SQL query" to the database server : it is actually using prepared statements, which means :

  • Sending the statement, for it to be prepared
  • And, then, sending the parameters
  • and executing the prepared statements

This means there is never a "real" SQL query on the PHP side -- so, Doctrine cannot display it.

share|improve this answer
$query->getSql(); –  machete Oct 18 '13 at 17:13
Guys, this answer is no longer relevant, read answers below. –  Jenechka Mar 4 at 8:05
add comment

You can check the query executed by your app if you log all the queries in mysql:


there will be more queries not only the one that you are looking for but you can grep for it.

but usually ->getSql(); works

share|improve this answer
query->getSql() worked well for me. Thanks –  netcrash Aug 29 '13 at 3:45
add comment

getSqlQuery() does technically show the whole SQL command, but it's a lot more useful when you can see the parameters as well.

echo $q->getSqlQuery();
foreach ($q->getFlattenedParams() as $index => $param)
  echo "$index => $param";

To make this pattern more reusable, there's a nice approach described in the comments at Raw SQL from Doctrine Query Object.

share|improve this answer
add comment

An example..

$qb = $this->createQueryBuilder('a');

Show SQL: $sql=$query->getSQL();

Show Parameters: $parameters=$query->getParameters();

share|improve this answer
This case works for Doctrine 2. –  j0k Nov 5 '12 at 14:21
add comment

There is no other real query, this is how prepared statements work. The values are bound in the database server, not in the application layer.

See my answer to this question: http://stackoverflow.com/questions/1786322/in-php-with-pdo-how-to-check-the-final-sql-parametrized-query/1786449#1786449

(Repeated here for convenience:)

Using prepared statements with parametrised values is not simply another way to dynamically create a string of SQL. You create a prepared statement at the database, and then send the parameter values alone.

So what is probably sent to the database will be a PREPARE ..., then SET ... and finally EXECUTE ....

You won't be able to get some SQL string like SELECT * FROM ..., even if it would produce equivalent results, because no such query was ever actually sent to the database.

share|improve this answer
add comment

Maybe it can be useful for someone:

// Printing the SQL with real values
$vals = $query->getFlattenedParams();
foreach(explode('?', $query->getSqlQuery()) as $i => $part) {
    $sql = (isset($sql) ? $sql : null) . $part;
    if (isset($vals[$i])) $sql .= $vals[$i];

echo $sql;
share|improve this answer
add comment

I have created a Doctrine2 Logger that does exactly this. It "hydrates" the parametrized sql query with the values using Doctrine 2 own data type conversors.


namespace Drsm\Doctrine\DBAL\Logging;
use Doctrine\DBAL\Logging\SQLLogger,
 * A SQL logger that logs to the standard output and
 * subtitutes params to get a ready to execute SQL sentence

 * @author  dsamblas@gmail.com
class EchoWriteSQLWithoutParamsLogger implements SQLLogger


    private $dbPlatform;
    private $loggedQueryTypes;
    public function __construct(AbstractPlatform $dbPlatform, array $loggedQueryTypes=array()){
     * {@inheritdoc}
    public function startQuery($sql, array $params = null, array $types = null)

                foreach ($params as $key=>$param) {
                    $sql = join(var_export($value, true), explode('?', $sql, 2));

            echo $sql . " ;".PHP_EOL;

     * {@inheritdoc}
    public function stopQuery()

    private function isLoggable($sql){
        if (empty($this->loggedQueryTypes)) return true;
        foreach($this->loggedQueryTypes as $validType){
            if (strpos($sql, $validType) === 0) return true;
        return false;

Usage Example:; The following peace of code will echo on standard output any INSERT,UPDATE,DELETE SQL sentences generated with $em Entity Manager,

/**@var  \Doctrine\ORM\EntityManager $em */
                    new EchoWriteSQLWithoutParamsLogger(
share|improve this answer
add comment

More clear solution:

 * Get string query 
 * @param Doctrine_Query $query
 * @return string
public function getDqlWithParams(Doctrine_Query $query){
    $vals = $query->getFlattenedParams();
    $sql = $query->getDql();
    $sql = str_replace('?', '%s', $sql);
    return vsprintf($sql, $vals);
share|improve this answer
add comment

Your Answer


