How do you view a DQL query prepared query at runt

2019-05-27 05:14发布

问题:

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;    
        }

回答1:

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.



回答2:

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);



回答3:

//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