如何在Phalcon中回显最后一个查询字符串? [英] How to echo last query string in Phalcon?
问题描述
我在codeigniter上做了很多工作.在codeigniter中,如果需要获取最后执行的查询字符串,我们可以使用以下方法获取它:
I have worked a lot on codeigniter. In codeigniter , if there is need to get query string that is executed last, we can get it using:
echo $this->db->last_query();
exit;
但是目前我正在研究Phalcon,而我只是这个框架的初学者.我很好奇是否有一种方法可以在phalcon中回显最后一个查询字符串.
But currently I am working on phalcon and I am just at beginner level in this framework. I am curious if there is a way to echo last query string in phalcon.
谢谢.
推荐答案
使用原始查询
让我们进行以下查询:
Using Raw Queries
Let us have the following query:
$phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id';
$this->db->execute($phql, ['id' => 1]);
我们可以通过以下方法获取调试查询信息:
We can get debug query info with the following methods:
print_r($this->db->getSQLStatement());
更新
news
设置category_id
= 5位置id
=:id
UPDATE
news
SETcategory_id
= 5 WHEREid
= :id
print_r($this->db->getSqlVariables());
数组( [id] => 1)
Array ( [id] => 1 )
有关DB方法的更多信息,可以在这里找到: https://docs. phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo.html
More info about DB methods you can find here: https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo.html
设置数据库连接和分析器服务:
Setting up your DB connection and profiler service:
use Phalcon\Db\Profiler as ProfilerDb;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Db\Adapter\Pdo\Mysql as MysqlPdo;
$di->set('profiler', function () {
return new ProfilerDb();
}, true);
$di->set('db', function () use ($di) {
$eventsManager = new EventsManager();
// Get a shared instance of the DbProfiler
$profiler = $di->getProfiler();
// Listen all the database events
$eventsManager->attach('db', function ($event, $connection) use ($profiler) {
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($connection->getSQLStatement());
}
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
$connection = new MysqlPdo(
array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
)
);
// Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
});
使用它来调试您的查询:
Using it to debug your Queries:
// Send some SQL statements to the database
Robots::find();
Robots::find(
array(
"order" => "name"
)
);
Robots::find(
array(
"limit" => 30
)
);
// Get the generated profiles from the profiler
$profiles = $di->get('profiler')->getProfiles();
foreach ($profiles as $profile) {
echo "SQL Statement: ", $profile->getSQLStatement(), "\n";
echo "Start Time: ", $profile->getInitialTime(), "\n";
echo "Final Time: ", $profile->getFinalTime(), "\n";
echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "\n";
}
有关Profiler服务的更多信息: https://docs.phalconphp.com/en/latest/reference/models.html#profiling-sql-statements
More info on Profiler service: https://docs.phalconphp.com/en/latest/reference/models.html#profiling-sql-statements
我正在使用FabianFülling制作的Phalcon可爱的调试小部件.您可以在此处检查存储库: https://github.com/fabfuel/prophiler 屏幕快照示例下面的操作中的小部件:
I'm using a lovely debug widget for Phalcon made by Fabian Fülling. You can check the repository here: https://github.com/fabfuel/prophiler A sample screen shot of the widget in action below:
这篇关于如何在Phalcon中回显最后一个查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!