一尘不染

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

mysql

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

    ...
                  $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查询吗?好心劝告


阅读 471

收藏
2020-05-17

共1个答案

一尘不染

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

从Zend Framework手册中:

$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/zh/zend.db.select.html

2020-05-17