如何在zend框架中打印精确的sql查询?

2022-08-30 08:32:29

我有以下代码,我从模型中获取,

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

当我在zend中使用更新查询时,例如,

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

在这里,我想知道确切的mysql查询。有没有办法在zend中打印mysql查询?亲切的建议


答案 1

选择对象在 Zend Framework 中有一个 __toString() 方法。

来自Zend框架手册:

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

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

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

另一种解决办法是使用Zend_Db_Profiler。即

$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


答案 2

从 >= 2.1.4

echo $select->getSqlString()

推荐