2011-09-07 70 views
2
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;  
     } 
+0

http://stackoverflow.com/questions/2095394/doctrine-how-to-print-out-the-real-sql-not-just-the-prepared-statment –

+0

我試過,但它不起作用在學說2? – jini

+1

在Doctrine 2中調試查詢最簡單的解決方案:$ em-> getConfiguration() - > setSQLLogger(new \ Doctrine \ DBAL \ Logging \ EchoSQLLogger()); –

回答

3

由Haim Evgi建議的EchoSqlLogger,echo's日誌輸出,所以你應該在你的網站上看到它。

如果你只是想看看有什麼SQL查詢主義會產生,使用:

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

但要注意,參數不包括在SQL字符串,它們顯示爲佔位符(=?)。

我使用的最常見的技術,以觀察什麼學說做的是啓用MySQL(或任何你使用的數據庫)query log(不要在負載很重的生產服務器上這樣做!)。

如果查詢日誌/var/log/mysql/query.log下,我只是這樣做:

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

(見tail command瞭解詳細信息)

並重新加載其執行查詢的頁面。

2

在教義2.3.2

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

您可以打印帶有的print_r($記錄器)$記錄;

0
//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 |整數