2011-10-11 9 views
58

Ho il seguente pezzo di codice che ho preso da modello,Come stampare la query sql esatta nel framework zend?

... 
        $select = $this->_db->select() 
        ->from($this->_name) 
        ->where('shipping=?',$type) 
        ->where('customer_id=?',$userid); 
       echo $select; exit; // which gives exact mysql query. 
      ..... 

Quando uso query di aggiornamento in Zend come,

$up_value = array('billing'=> '0'); 
$this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']);  

Qui voglio sapere la query mysql esatto. C'è un modo possibile per stampare la query mysql in zend? consiglio gentile

risposta

107

Selezionare gli oggetti hanno un metodo __toString() in Zend Framework.

Dal manuale di Zend Framework:

$select = $db->select() 
      ->from('products'); 

$sql = $select->__toString(); 
echo "$sql\n"; 

// The output is the string: 
// SELECT * FROM "products" 

Una soluzione alternativa sarebbe quella di utilizzare lo Zend_Db_Profiler. cioè

$db->getProfiler()->setEnabled(true); 

// your code 
$this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']); 

Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery()); 
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams()); 
$db->getProfiler()->setEnabled(false); 

http://framework.zend.com/manual/en/zend.db.select.html

+0

Grazie, ho bisogno di stampare la query di aggiornamento in zend framwork.come posso fare come l'oggetto select.Do abbiamo qualsiasi metodo per stampare la query sql per il metodo di aggiornamento in zend – mymotherland

+0

è possibile utilizzare Zend_Db_Profiler per quello, vedere la risposta aggiornata –

0

Utilizzare questa: -

echo $select->query(); 

o

Zend_Debug::dump($select->query(); 
+0

Grazie vascowhite. Ma ho bisogno di conoscere il formato di query mysql aggiornamento non un formato di query select.how per stampare questo "$ questo-> aggiornamento ...." – mymotherland

+0

Non penso che SQL viene memorizzato ovunque, viene generato ed eseguito. – vascowhite

+0

Grazie ma solo 2o approccio 'Zend_Debug :: dump ($ select-> query();' ha funzionato –

6

È possibile utilizzare Zend_Debug::Dump($select->assemble()); per ottenere la query SQL.

Oppure è possibile abilitare Zend DB FirePHP profiler che consente di ottenere tutte le query in un formato ordinato in Firebug (anche istruzioni UPDATE).

EDIT: profilatura con FirePHP funziona anche anche in FF6.0 + (non solo in FF3.0 come suggerito nel link)

0

Scopri i Zend_Db_Profiler. Ciò consente di registrare qualsiasi istruzione SQL appena viene preparata ed eseguita. Funziona sia per le istruzioni UPDATE che per le query SELECT.

13

Ho attraversato centinaia di pagine, su Google molto, ma non ho trovato alcuna soluzione esatta. Finalmente questo ha funzionato per me. Indipendentemente da dove ti trovi in ​​un controller o in un modello. Questo codice ha funzionato per me ovunque. Basta usare questo

//Before executing your query 
$db = Zend_Db_Table_Abstract::getDefaultAdapter(); 
$db->getProfiler()->setEnabled(true); 
$profiler = $db->getProfiler(); 

// Execute your any of database query here like select, update, insert 
//The code below must be after query execution 
$query = $profiler->getLastQueryProfile(); 
$params = $query->getQueryParams(); 
$querystr = $query->getQuery(); 

foreach ($params as $par) { 
    $querystr = preg_replace('/\\?/', "'" . $par . "'", $querystr, 1); 
} 
echo $querystr; 

Finalmente questa cosa ha funzionato per me.

2

è possibile stampare ..

print_r($select->assemble()); 
20

da> = 2.1.4

echo $select->getSqlString() 
+0

Come è venuto a sapere questo? –

+0

Questa non è una buona soluzione ... cita tutti i valori nel formato sbagliato .. –

0
$statement = $this->sql->getSqlStringForSqlObject(HERE GOES Zend\Db\Sql\SelectSQL object); 

echo "SQL statement: $statement"; 

Esempio:

$select = $this->sql->select(); 
... 
$select->from(array('u' => 'users')); 
$select->join(... 
$select->group('u.id'); 
... 
$statement = $this->sql->getSqlStringForSqlObject($select); 
echo $statement; 
-1
$db->getProfiler()->setEnabled(true); 

// your code  
$this->update('table', $data, $where);  
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());  
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());  
$db->getProfiler()->setEnabled(false); 
0

ho fatto thi s in questo modo

$sql = new Sql($this->adapter); 
     $select = $sql->select(); 
     $select->from('mock_paper'); 
     $select->columns(array(
      'is_section' 
     )); 
     $select->where(array('exam_id = ?' => $exam_id,'level_id = ?' => $level_id))->limit(1); 



     $sqlstring = $sql->buildSqlString($select); 
     echo $sqlstring; 
     die(); 
0

ancora più breve:

echo $select->__toString()."\n"; 

e più più breve:

echo $select .""; die;