我想查看下面即将执行的SQL语句:
<?php
//Deleting existing robot
$success = $connection->delete(
"robots",
"id = 101"
);
//Next SQL sentence is generated
DELETE FROM `robots` WHERE `id` = 101
如何添加某种监听器或只是简单的 var_dump 将由 $connection->delete 生成的选择查询
谢谢
最佳答案
我决定使用记录器类和事件系统:Phalcon Events Manager
您创建一个扩展记录器适配器的类
<?php
namespace PhalconX\Logger\Adapter;
/**
* Basic Array based Logging for debugging Phalcon Operations
* @package PhalconX\Logger\Adapter
*/
class Basic extends \Phalcon\Logger\Adapter
{
private $data = array();
/**
* Add a statement to the log
* @param string $statement
* @param null $type
* @param array $params
* @return $this|\Phalcon\Logger\Adapter
*/
public function log($statement, $type=null, array $params=null)
{
$this->data[] = array('sql'=>$statement, 'type'=>$type, 'params'=>$params); // array('sql'=>$statement, 'type'=>$type);
return $this;
}
/**
* return the log
* @return array
*/
public function getLog(){
return $this->data;
}
/**
* Required function for the interface, unused
* @param $message
* @param $type
* @param $time
* @param $context
*/
public function logInternal($message, $type, $time, $context){
}
/**
* Required function for the interface, unused
*/
public function getFormatter(){
}
/**
* Required function for the interface, unused
*/
public function close(){
}
}
然后将其附加到您的数据库,并按类型探索事件
$eventsManager = new \Phalcon\Events\Manager();
$logger = new \PhalconX\Logger\Adapter\Basic();
$profiler = $phalconDi->getProfiler();
//Listen all the database events
/** @var $event \Phalcon\Events\Event */
/** @var $phalconConnection \Phalcon\Db\Adapter\Pdo\Mysql */
$eventsManager->attach('db', function($event, $phalconConnection) use ($logger, $profiler) {
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($phalconConnection->getSQLStatement());
$logger->log($phalconConnection->getSQLStatement(), \Phalcon\Logger::INFO, $phalconConnection->getSQLVariables());
}
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
这假设您的依赖注入(inject)器中有一个“db”键。
我的记录器只是将查询存储在一个数组中,这样我就可以在页面底部输出它们。
关于php - 如何在 Phalconphp 中查看 SQL 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24886296/