function get_event($id){
$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');
$query->setParameter('id', $id);
//CAN I VIEW THE QUERY AT THIS TIME?
$result = $query->getResult();
return $result;
}

- 11,155
- 36
- 98
- 169
-
http://stackoverflow.com/questions/2095394/doctrine-how-to-print-out-the-real-sql-not-just-the-prepared-statment – Haim Evgi Sep 07 '11 at 05:40
-
I tried that but it does not work in doctrine 2? – Strong Like Bull Sep 07 '11 at 06:10
-
1Most simple solution for debugging queries in Doctrine 2:$em->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); – Haim Evgi Sep 07 '11 at 06:12
3 Answers
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);

- 81
- 1
- 2
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.

- 15,693
- 14
- 81
- 131
//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

- 3,807
- 1
- 25
- 22