dreamlife2014 2016-08-16 06:24
浏览 97
已采纳

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

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;

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.

Thank you.

  • 写回答

2条回答 默认 最新

  • douling0053 2016-08-16 06:41
    关注

    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());
    

    UPDATE news SET category_id = 5 WHERE id = :id

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

    Array ( [id] => 1 )

    More info about DB methods you can find here: https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo.html


    Working with Models

    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(), "
    ";
       echo "Start Time: ", $profile->getInitialTime(), "
    ";
       echo "Final Time: ", $profile->getFinalTime(), "
    ";
       echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "
    ";
    }
    

    More info on Profiler service: https://docs.phalconphp.com/en/latest/reference/models.html#profiling-sql-statements


    Phalcon Prophiler Widget

    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:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?