Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the sql from a createQueryBuilder?

Do you know how I can make to get the 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();
    }

Thanks you all for your help. Best regards

EDIT

To get the sql, you have to make :

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

Thanks all !

like image 903
Dimitri Avatar asked Mar 07 '14 16:03

Dimitri


2 Answers

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

like image 63
Javad Avatar answered Oct 11 '22 23:10

Javad


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

echo $query->getQuery()->getSql();
like image 42
M Khalid Junaid Avatar answered Oct 12 '22 01:10

M Khalid Junaid