[英]Laravel 5.4 - How to set PDO Fetch Mode?
The ability to customize the fetch mode was removed from L5.4 and is defaulted to PDO::FETCH_OBJ.自定义获取模式的功能已从 L5.4 中删除,默认为 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? 1)我应该在哪里放置代码? Should I register it with the EventServiceProvider
?我应该用EventServiceProvider
注册它吗?
2) When does the StatementPrepared
event fire? 2) StatementPrepared
事件何时触发? (I only need to change the Fetch Mode for specific repository functions, not on a global scale). (我只需要为特定的存储库功能更改 Fetch Mode,而不是在全局范围内)。
3) Does the FetchMode revert itself automatically for subsequent queries? 3) FetchMode 是否会为后续查询自动恢复?
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转到: 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);
});
$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;).您只需要 DB trait(使用 DB;)。
Add default fetch mode in config/database.php在 config/database.php 中添加默认获取模式
return [
'fetch' => PDO::FETCH_CLASS,
...
];
Theres another option I find to by pass it Add env我发现还有另一个选项可以绕过它添加环境
DB_FETCHMODE=FETCH_ASSOC
In config/database add for connections.mysql在 config/database 添加connections.mysql
'fetch_mode' => env('DB_FETCHMODE', 'FETCH_ASSOC'),
In illuminate/datbase/connection.php replace prepared function with在illuminate/datbase/connection.php 中,将准备好的函数替换为
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这为您的应用程序设置了默认的FETCH_ASSOC
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.