Laravel 5.4 - How to set PDO Fetch Mode?

2020-02-28 14:47发布

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!

4条回答
▲ chillily
2楼-- · 2020-02-28 15:34

Add default fetch mode in config/database.php

return [
    'fetch' => PDO::FETCH_CLASS,
    ...
];
查看更多
一夜七次
3楼-- · 2020-02-28 15:38

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);
});
查看更多
放荡不羁爱自由
4楼-- · 2020-02-28 15:40
$dbh=DB::getPdo();
$sth = $dbh->prepare("SHOW STATUS
            WHERE Variable_name = 'Max_used_connections'
            OR Variable_name = 'Max_used_connections_time'
            OR Variable_name = 'Threads_connected' ");
$sth->execute(); 
$result = $sth->fetch(PDO::FETCH_CLASS);
print_r($result);

Try this. worked for me. You require only DB trait(use DB;).

查看更多
虎瘦雄心在
5楼-- · 2020-02-28 15:49

Theres another option I find to by pass it Add env DB_FETCHMODE=FETCH_ASSOC
In config/database add for connections.mysql 'fetch_mode' => env('DB_FETCHMODE', 'FETCH_ASSOC'),
In illuminate/datbase/connection.php replace prepared function with
protected function prepared (PDOStatement $statement){ $config = $this->config; $statement->setFetchMode($config['fetch_mode'] == "FETCH_OBJ" ? 5 : ($config['fetch_mode'] == "FETCH_NUM" ? 3 : 2)); $this->event(new Events\StatementPrepared( $this, $statement )); return $statement; }

This make default FETCH_ASSOC for your application

Then if you want to change it like before, add
config(['database.connections.mysql.fetch_mode' => 'FETCH_OBJ']);
a replacement of DB::setFetchMode(PDO::FETCH_ASSOC);

查看更多
登录 后发表回答