The ability to configure the fetch mode was removed from L5.4 and the default command PDO :: FETCH_OBJ was set.
The upgrade guide states that you can override this using an event listener:
Event::listen(StatementPrepared::class, function ($event) { $event->statement->setFetchMode(...); });
I canβt understand in life how to implement this:
1) Where should I put the code? Should I register it using EventServiceProvider ?
2) When does the StatementPrepared event fire? (I need to change the Fetch mode for specific repository functions, not globally).
3) Does FetchM automatically return automatically for subsequent requests?
Here is an example of my code:
<?php namespace App\Repositories\Backend; use DB; use PDO; class SystemRepository { 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; } }
Thanks!
source share