How do you view a DQL query prepared query at runtime?

孤街浪徒 提交于 2019-12-06 11:30:26

The EchoSqlLogger as suggested by Haim Evgi, well, echo´s the log output, so you should see it on your website.

If you just want to see what SQL query Doctrine would generate, use:

$query = $this->em->createQuery('SELECT e.name,e.date, e.time, e.venue, e.venueaddress,e.parish,e.genre, e.entryprice, e.phone, e.specialguests,
                                        e.weblink, e.otherinfo, e.flyer1, e.flyer2 from Events e WHERE e.id = :id');

print $query->getSQL();

But be aware, parameters are not included in that sql string, they are shown as placeholders (= ?).

The most common technique I use in order to watch what Doctrine does is enabling the mysql (or whatever db you use) query log (dont do this on a production server which is under heavy load!).

If the query log is under /var/log/mysql/query.log I just do this:

# tail -f /var/log/mysql/query.log

(see tail command for more details)

And reload the page which executes the query.

In doctrine 2.3.2

use Doctrine\DBAL\Logging\DebugStack;
$logger = new DebugStack();
$config->setSQLLogger($logger);

now you can print $logger with print_r($logger);

//create query
$oQuery = $this->em->createQuery('SELECT tbl.id FROM mytable tbl WHERE tbl.id = :id');

//print query and params
echo "SQL: ".$oQuery->getSQL();
while($param = $oQuery->getParameters()->current()){
  echo "Param: ".$param->getName()." | ".$param->getValue()." | ".$param->getType();
  $oQuery->getParameters()->next();
}

SQL: SELECT tbl.id AS id0 FROM mytable tbl WHERE tbl.id = ?

Param: id | 1 | integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!