phalcon:跟踪sql语句
来源:互联网 发布:网络写字员兼职 编辑:程序博客网 时间:2024/05/20 06:08
phalcon没有像yii那些框架一样内置trace工具,所以我们只能自己搞。
在phalcon里有一个\Phalcon\Db\Profiler 类,这个类可以用来记录sql语句并计算消耗的时间。
那么如何使用它呢?
手册里其实已经提供了方法,总结如下:
1.向$di里注册profiler服务
1
2
3
$di
->set(
'profiler'
,
function
(){
return
new
\Phalcon\Db\Profiler();
}, true);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$di
->set(
'db'
,
function
()
use
(
$di
) {
//新建一个事件管理器
$eventsManager
=
new
\Phalcon\Events\Manager();
//从di中获取共享的profiler实例
$profiler
=
$di
->getProfiler();
//监听所有的db事件
$eventsManager
->attach(
'db'
,
function
(
$event
,
$connection
)
use
(
$profiler
) {
//一条语句查询之前事件,profiler开始记录sql语句
if
(
$event
->
getType
() ==
'beforeQuery'
) {
$profiler
->startProfile(
$connection
->getSQLStatement());
}
//一条语句查询结束,结束本次记录,记录结果会保存在profiler对象中
if
(
$event
->
getType
() ==
'afterQuery'
) {
$profiler
->stopProfile();
}
});
$connection
=
new
\Phalcon\Db\Adapter\Pdo\Mysql(
array
(
"host"
=>
"localhost"
,
"username"
=>
"root"
,
"password"
=>
"secret"
,
"dbname"
=>
"invo"
));
//将事件管理器绑定到db实例中
$connection
->setEventsManager(
$eventsManager
);
return
$connection
;
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//执行一些查询
Robots::find();
Robots::find(
array
(
"order"
=>
"name"
));
Robots::find(
array
(
"limit"
=> 30));
//获取所有的prifler记录结果,这是一个数组,每条记录对应一个sql语句
$profiles
=
$this
->di->get(
'profiler'
)->getProfiles();
//遍历输出
foreach
(
$profiles
as
$profile
) {
echo
"SQL语句: "
,
$profile
->getSQLStatement(),
"\n"
;
echo
"开始时间: "
,
$profile
->getInitialTime(),
"\n"
;
echo
"结束时间: "
,
$profile
->getFinalTime(),
"\n"
;
echo
"消耗时间: "
,
$profile
->getTotalElapsedSeconds(),
"\n"
;
}
//直接获取最后一条sql语句
echo
$this
->di->get(
'profiler'
)->getLastProfile()->getSQLStatement();
转载自:http://my.oschina.net/cxz001/blog/289776
0 0
- phalcon:跟踪sql语句
- phalcon:跟踪sql语句
- oracle跟踪sql语句
- mysql执行sql语句跟踪
- MySQL如何跟踪sql语句
- SQL: 跟踪SQL语句的执行情况
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何跟踪mysql执行的sql语句
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- 如何用T-SQL语句建立跟踪
- redis 快速入门实战
- android4.4 ROM开发者全盘扫描解决方案
- OpenCV矩阵运算
- HDU 1010 Tempter of the Bone
- VS2010静态编译安装Qt5.1.1+openssl
- phalcon:跟踪sql语句
- 数据结构——赫夫曼编码
- ctemplate模板
- VS2010+DCMTK3.6.0 : MT支持库 安装说明
- Rsync、Unison及DRBD的比较
- HDU 2084 数塔(DP)
- 225.Implement Stack using Queues (用队列实现栈)
- eclipse启动tomcat服务几种错误总结
- ios总结(多页面跳转:)