【问题标题】:How to echo last query string in Phalcon?如何在 Phalcon 中回显最后一个查询字符串?
【发布时间】:2016-12-22 09:58:18
【问题描述】:

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

echo $this->db->last_query();
exit;

但目前我正在研究 phalcon,而且我只是在这个框架中处于初级水平。我很好奇是否有办法在 phalcon 中回显最后一个查询字符串。

谢谢。

【问题讨论】:

    标签: php mysql sql phalcon


    【解决方案1】:

    使用原始查询

    让我们有以下查询:

    $phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id';
    $this->db->execute($phql, ['id' => 1]);
    

    我们可以通过以下方法获取调试查询信息:

    print_r($this->db->getSQLStatement());
    

    更新news SET category_id = 5 WHERE id = :id

    print_r($this->db->getSqlVariables());
    

    数组 ( [id] => 1)

    您可以在此处找到有关 DB 方法的更多信息:https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo.html


    使用模型

    设置您的数据库连接和分析器服务:

    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";
    }
    

    有关 Profiler 服务的更多信息:https://docs.phalconphp.com/en/latest/reference/models.html#profiling-sql-statements


    Phalcon Prophiler 小部件

    我正在使用 Fabian Fülling 为 Phalcon 制作的一个可爱的调试小部件。您可以在此处查看存储库:https://github.com/fabfuel/prophiler 以下小部件的示例屏幕截图:

    【讨论】:

    • 我在return $this->_userEntriesEntries->find(array("conditions" => "FeaturedPost = 1 and FeaturedPostStatus = 1", "order" => "ID DESC", "limit" => 4));,现在该怎么办?
    • 我已经更新了我的答案,请阅读关于如何设置它的简短指南。
    • 我赞成这个答案,因为它比我发布的要好得多。 (我的很脏,适合懒人
    • 有没有办法在绑定后打印查询或打印绑定参数
    【解决方案2】:

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

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

    var_dump 查询的结果对象。在 PDO 转储中,您会注意到一个名为 _pdoStatement 的键。这是您生成的 SQL 查询。

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

    【讨论】:

    • 谢谢,作为懒惰的人,这就是我想要的。谢谢。 @蒂莫西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 2021-11-22
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多