Php 如何回显Phalcon中的最后一个查询字符串?

Php 如何回显Phalcon中的最后一个查询字符串?,php,mysql,sql,phalcon,Php,Mysql,Sql,Phalcon,我在codeigniter上做了很多工作。在codeigniter中,如果需要获取最后执行的查询字符串,我们可以使用: echo $this->db->last_query(); exit; 但目前我正在研究phalcon,在这个框架中我只是处于初级阶段。我很好奇是否有办法回显phalcon中的最后一个查询字符串 多谢各位 使用原始查询 让我们提出以下问题: $phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id'

我在codeigniter上做了很多工作。在codeigniter中,如果需要获取最后执行的查询字符串,我们可以使用:

echo $this->db->last_query();
exit;
但目前我正在研究phalcon,在这个框架中我只是处于初级阶段。我很好奇是否有办法回显phalcon中的最后一个查询字符串

多谢各位

使用原始查询 让我们提出以下问题:

$phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id';
$this->db->execute($phql, ['id' => 1]);
我们可以通过以下方法获取调试查询信息:

print_r($this->db->getSQLStatement());
更新
news
SET
category\u id
=5其中
id
=:id

排列( [id]=>1)

有关DB方法的更多信息,请参见:


与模型一起工作 设置数据库连接和探查器服务:

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;
});
使用它调试查询:

// 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";
}
有关探查器服务的详细信息:


Phalcon Prophiler小部件 我正在使用Fabian Fülling为Phalcon制作的一个可爱的调试小部件。您可以在此处查看存储库:下面是正在运行的小部件的示例屏幕截图:

使用原始查询 让我们提出以下问题:

$phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id';
$this->db->execute($phql, ['id' => 1]);
我们可以通过以下方法获取调试查询信息:

print_r($this->db->getSQLStatement());
更新
news
SET
category\u id
=5其中
id
=:id

排列( [id]=>1)

有关DB方法的更多信息,请参见:


与模型一起工作 设置数据库连接和探查器服务:

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;
});
使用它调试查询:

// 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";
}
有关探查器服务的详细信息:


Phalcon Prophiler小部件 我正在使用Fabian Fülling为Phalcon制作的一个可爱的调试小部件。您可以在此处查看存储库:下面是正在运行的小部件的示例屏幕截图:


如果您直接在模型实例上运行查询,并且您很懒,那么您也可以这样做:

$result = $this->_userEntriesE‌​ntries->find(array("c‌​onditions" => "FeaturedPost = 1 and FeaturedPostStatus = 1", "order" => "ID DESC", "limit" => 4))

var_dump($result);
var\u dump
查询的结果对象。在PDO转储中,您会注意到一个名为
\pdoStatement
的键。这是您生成的SQL查询


这不是推荐的方法,只是一个肮脏的把戏。

如果您直接在模型实例上运行查询,并且您很懒惰,您也可以这样做:

$result = $this->_userEntriesE‌​ntries->find(array("c‌​onditions" => "FeaturedPost = 1 and FeaturedPostStatus = 1", "order" => "ID DESC", "limit" => 4))

var_dump($result);
var\u dump
查询的结果对象。在PDO转储中,您会注意到一个名为
\pdoStatement
的键。这是您生成的SQL查询


这不是推荐的方法,只是一个肮脏的把戏。

我在
return$This->\u userEntriesEntries->find(数组(“条件”=>“FeaturedPost=1和FeaturedPostStatus=1”,“订单”=>“ID描述”,“限制”=>4))我现在该怎么办?我已经更新了我的答案,请阅读关于如何设置它的简短指南。我准备投票给这个答案,因为它比我发布的答案要好得多。(我的是脏的,适合懒惰的人)有没有办法在绑定后打印查询,或者在
return$this->\u userEntriesEntries->find处打印绑定参数(数组(“条件”=>“FeaturedPost=1和FeaturedPostStatus=1”,“顺序”=>“ID DESC”,“限制”=>4))我现在该怎么办?我已经更新了我的答案,请阅读关于如何设置它的简短指南。我准备投票给这个答案,因为它比我发布的答案要好得多。(我的是脏的,适合懒惰的人)有没有一种方法可以在绑定后打印查询,或者打印绑定参数呢?作为懒惰的人,这就是我一直在寻找的。谢谢@TimothyThanks,作为懒惰的人,这就是我想要的。谢谢@提摩太