Laravel 5.4 - 如何设置PDO提取模式? [英] Laravel 5.4 - How to set PDO Fetch Mode?

查看:887
本文介绍了Laravel 5.4 - 如何设置PDO提取模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义提取模式的功能已从L5.4中删除,默认为PDO :: FETCH_OBJ。



升级指南说明可以通过使用事件监听器:

 事件:: listen(StatementPrepared :: class,function($ event){
$ event-> statement-> setFetchMode(...);
});

我无法理解我的生活如何实现:



1)我应该在哪里放置代码?我应该使用 EventServiceProvider 注册吗?

2) StatementPrepared 事件何时触发? (我只需要更改特定存储库功能的获取模式,而不是在全局范围内更改)。

3)FetchMode是否自动为后续查询自动恢复?



以下是我的代码示例:

 <?php 

命名空间应用\Repositories\Backend;

使用DB;
使用PDO;

class SystemRepository
{
/ **
*获取连接状态变量。
*
* @return array
* /
public function getConnectionStatus()
{
DB :: connection('backend') - > setFetchMode (PDO :: FETCH_ASSOC);

$ result = DB :: connection('backend')
- > select(DB :: raw(
SHOW STATUS
WHERE Variable_name ='Max_used_connections '
OR Variable_name ='Max_used_connections_time'
OR Variable_name ='Threads_connected'
))
;

DB :: connection('backend') - > setFetchMode(PDO :: FETCH_CLASS);

return $ result;
}
}

谢谢!


这到文件的顶部:

 使用Illuminate\Database\Events\StatementPrepared; 

启动方法中添加:

  Event :: listen(StatementPrepared :: class,function($ event){
$ event-> statement-> setFetchMode(\PDO :: FETCH_ASSOC);
});


The ability to customize the fetch mode was removed from L5.4 and is defaulted to PDO::FETCH_OBJ.

The upgrade guide states that you can override this by using an event listener:

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(...);
});

I can't for the life of me understand how to implement this:

1) Where should I place the code? Should I register it with the EventServiceProvider?
2) When does the StatementPrepared event fire? (I only need to change the Fetch Mode for specific repository functions, not on a global scale).
3) Does the FetchMode revert itself automatically for subsequent queries?

Here's an example of my code:

<?php

namespace App\Repositories\Backend;

use DB;
use PDO;

class SystemRepository
{
    /**
     * Get the connection status variables.
     *
     * @return array
     */
    public function getConnectionStatus()
    {
        DB::connection('backend')->setFetchMode(PDO::FETCH_ASSOC);

        $result = DB::connection('backend')
            ->select(DB::raw("
                SHOW STATUS
                WHERE Variable_name = 'Max_used_connections'
                OR Variable_name = 'Max_used_connections_time'
                OR Variable_name = 'Threads_connected'
            "))
        ;

        DB::connection('backend')->setFetchMode(PDO::FETCH_CLASS);

        return $result;
    }
}

Thank you!

解决方案

Go to: app/Providers/EventServiceProvider.php

Add this to the top of the file:

use Illuminate\Database\Events\StatementPrepared;

In the boot method add:

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
});

这篇关于Laravel 5.4 - 如何设置PDO提取模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆