我想查看即将在下面执行的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-&gt; delete
生成的select查询由于
答案 0 :(得分:2)
我决定使用记录器类和事件系统: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();
}
});
这假设你有一个&#39; db&#39;依赖注入器中的键。
我的记录器只是将查询存储在一个数组中,因此我可以在页面底部输出它们。
答案 1 :(得分:1)
我将那些准备好的SQL语句与最接近真实的SQL语句分开的技巧:
<ion-nav-bar>
定义为方法或函数,您可以将其结果推送到分析器,如接受的答案:
function statement2sql($connection) {
$stmt = $connection->getSQLStatement();
foreach ( $connection->getSQLVariables() as $k => $v ) {
// replaces :p1, .. :p11 .. and defined binds with binded values
$stmt = preg_replace('/:' . $k . '([^A-Za-z0-9])/', '\'' . $v . '\'$1', $stmt);
}
return $stmt;
}
或以其他方式存储 - 使用记录器或其他调试类。
答案 2 :(得分:0)
我很幸运在try / catch中包装我的SQL执行调用,然后打印异常。 MySQL返回的任何错误消息都在异常消息中,该消息将包含原始查询。
function get_item_by_id ($db_connection, $item_id) {
try {
$stmt = 'SELECT * FROM inventory WHERE id=:id';
$prepared_stmt = $db_connection->prepare ($stmt);
$result = $db_connection->executePrepared ($prepared_stmt,
array (
"id" => $item_id
),
array (
"id" => Column::BIND_PARAM_INT
)
);
$result->setFetchMode (Phalcon\Db::FETCH_OBJ);
$item_arr = $result->fetchAll ();
return $item_arr;
}
catch (Exception $e) {
print_r ($e->getMessage());
}
}
答案 3 :(得分:-1)
另一个选择,我个人的偏好,是从数据库的角度来看待这种情况。大多数SQL数据库允许您为某些事件(在您的情况下为DELETE)设置触发器,并生成包含传入请求的完整文本的日志条目。