How to get sql from createQueryBuilder?

Do you know how can I do to get sql from my createQueryBuilder?

My Entity / DownloadRepository.php class:

public function getLastDownload($limit) { $query = $this->createQueryBuilder('d'); $query->select('l.ytId, d.title, d.date, d.id, l.creator') ->from('DimiYvmBundle:Log', 'l') ->where('d.ytId = l.ytId AND l.creator = :creator') ->orderBy('l.id', 'DESC') ->groupBy('l.ytId') ->setParameter('creator', 'n') ->setMaxResults($limit); // echo $query->getSQL(); => Doesn't work... return $query->getQuery()->getResult(); } 

Thank you all for your help. Best wishes

EDIT

To get sql you have to do:

 echo $query->getQuery()->getSql(); 

Thanks everyone!

+7
php symfony doctrine doctrine2
source share
2 answers

You can get from $query->getQuery()->getSQL() Just keep in mind what will it produce for parameters ? instead of value.
If you want to receive a full SQL query with parameters and values, check the profiler toolbar in DEV mode of your page in the browser.

+15
source share

You need to call getSql() on the getQuery() object

 echo $query->getQuery()->getSql(); 
+3
source share

All Articles