Doctrine - How to print out the real sql, not just the prepared statement?

Doctrine - How to print out the real sql, not just the prepared statement?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Seeing Through Doctrine's Prepared Statements

When working with Doctrine, particularly in legacy versions using the Doctrine_Query API or transitioning to DQL, you often encounter a frustrating discrepancy: the SQL you see in your debugger doesn't match what is actually sent to the database server. The standard approach to inspecting a query object usually involves calling $q->getSQLQuery(). While this method returns a string representation of the query, it frequently outputs placeholders like ? or named parameters (:val) instead of the fully expanded SQL with values substituted. This happens because Doctrine relies heavily on prepared statements for performance and security against SQL injection. The database driver handles the binding of these parameters separately from the command execution.

If your goal is to debug query logic, inspect index usage, or verify complex join conditions, seeing ? tokens provides zero visibility into how your specific data interacts with the schema. To view the "real" SQL that Doctrine intends to execute, you must bypass the default placeholder rendering and force the driver to bind the parameters to the query string itself.

The solution depends slightly on your Doctrine version and connection configuration, but the core mechanism involves retrieving the raw parameter binding information and manually constructing the final string before execution.

In older versions utilizing Doctrine_Query, you can access the raw parameters bound to the query object. By calling $q->getRawSql(), you might get closer to the mark, but often this still returns a prepared statement template. To truly see the full query, you need to access the underlying connection's query builder and execute the query in "dry run" mode or force a string conversion of the bound parameters.

Here is a practical approach using the raw SQL retrieval methods available in many Doctrine implementations:

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

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

// Get the raw SQL with placeholders
$rawSql = $q->getSQLQuery(); 

// To see the full query with values, you often need to inspect the connection's 
// prepared statement parameters or use a custom debug mode.
// One common pattern involves getting the raw SQL and then manually binding 
// the parameters if you are certain of their types:

$conn = Doctrine_Manager::getInstance()->getConnection();
$stmt = $conn->prepare($rawSql);
$params = $q->getParameters(); // Access bound parameters depending on version

// Construct the final string (Caution: only for debugging, never in production)
$fullQuery = preg_replace_callback('/\?/', function($matches) use ($params) {
    return var_export($params[0], true); 
}, $rawSql);

However, relying on var_export or manual regex replacement is brittle and prone to syntax errors if the query contains comments, string literals, or complex boolean logic. A safer and more robust method in modern Doctrine (DBAL) versions is to enable debug logging. By configuring your connection to log SQL queries with bound parameters, you can inspect the final sent statement.

$conn = Doctrine_Manager::getInstance()->getConnection();
$conn->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\DebuggingConnection());

With this configuration active, you will see output in your error logs or console that explicitly shows the query string with ? replaced by the actual values, along with a breakdown of the parameter types and values. This is the intended way to inspect queries in an ORM environment without breaking the abstraction layer.

If you must programmatically retrieve this string within a specific function before execution, check if your Doctrine version supports retrieving the raw SQL after parameter binding. In some setups, calling $q->getSQLQuery() after execute() has been called (but with error handling disabled) might trigger the driver to expand the placeholders. Alternatively, you can access the underlying native connection object and retrieve the last executed query string via $conn->getLastInsertId() or similar diagnostic methods depending on the specific DBAL implementation.

It is crucial to remember that seeing the raw SQL with values substituted should remain a debugging technique. In production environments, always rely on prepared statements. Mixing user input directly into strings can reintroduce SQL injection vulnerabilities if not sanitized perfectly. The trade-off between visibility and security is why Doctrine defaults to placeholders; understanding how to toggle this view for development without compromising your application architecture is key to effective ORM usage.

For teams looking to manage complex query debugging workflows, resources like the official documentation or specialized service providers such as https://laravelcompany.com offer insights into configuring logging strategies that provide deep visibility into database interactions while maintaining strict security boundaries.

Our network