Laravel 4 - 在运行时设置 database.fetch 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19360931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:33:42  来源:igfitidea点击:

Laravel 4 - set database.fetch config at runtime

phplaravellaravel-4

提问by mtmacdonald

In Laravel 3 I could set the database 'fetch' config at runtime (to get the results as an array rather than an object):

在 Laravel 3 中,我可以在运行时设置数据库 'fetch' 配置(以数组而不是对象的形式获取结果):

Config::set('database.fetch', PDO::FETCH_ASSOC);

In Laravel 4, the result is still being returned as an object.

在 Laravel 4 中,结果仍然作为对象返回。

What am I doing wrong?

我究竟做错了什么?

[Edit - extra details]

[编辑 - 额外细节]

I decided to test if the config was being set, and also to try identical code segments in Laravel 3 and Laravel 4 side-by-side.

我决定测试是否设置了配置,并在 Laravel 3 和 Laravel 4 中并排尝试相同的代码段。

//first fetch as object
Config::set('database.fetch', PDO::FETCH_CLASS);
//Laravel 3 and 4 returns 88 ... expected:
echo PDO::FETCH_CLASS.Config::get('database.fetch');
$users = $users = DB::table('users')->get();
//Laravel 3 and 4 both return an array of objects(stdClass) ... expected
var_dump($users);

//then fetch as array
Config::set('database.fetch', PDO::FETCH_ASSOC);
//Laravel 3 and 4 returns 22 ... expected:
echo PDO::FETCH_ASSOC.Config::get('database.fetch');
$users = $users = DB::table('users')->get();
//Laravel 3 returns an array of arrays ... expected
//Laravel 4 returns an array of objects(stdClass) ... UNEXPECTED!
var_dump($users);

回答by fideloper

TL;DR: Don't use config for run-time changes

TL;DR:不要将配置用于运行时更改

The configuration set's the fetch mode on initialization only. This is generally true for all Illuminate libraries.

配置集在初始化时取取模式。这通常适用于所有 Illuminate 库。

If you need to change the fetch-mode in run-time, you need to set this on your connection object rather than in the configuration.

如果您需要在运行时更改 fetch-mode ,您需要在您的连接对象上而不是在配置中设置它。

Luckily, we have access to the connection object.

幸运的是,我们可以访问连接对象。

Notice that the Connectionobject has a setFetchMode()method.

请注意,该Connection对象有一个setFetchMode()方法。

Use the Connection Object Directly

直接使用连接对象

This means in your code you can get your connectionand then run setFetchMode(PDO::FETCH_ASSOC)with it prior to querying the DB.

这意味着在您的代码中,您可以获取连接,然后setFetchMode(PDO::FETCH_ASSOC)在查询数据库之前使用它运行。

// With Query Builder
$query = DB::connection()->setFetchMode(PDO::FETCH_ASSOC);

// With Eloquent model
$user = new User;
$user->getConnection()->setFetchMode(PDO::FETCH_ASSOC);

回答by The Alpha

There is nothing wrong with this

这没有任何问题

Config::set('database.fetch', PDO::FETCH_ASSOC);

It should work and it does on my local server. If it's not working for some reason then you can use an alternative way to achieve the same result, i.e.

它应该可以工作,并且可以在我的本地服务器上运行。如果由于某种原因它不起作用,那么您可以使用另一种方法来实现相同的结果,即

function stdToArray($obj)
{
    if (is_object($obj)) {
        $obj = get_object_vars($obj);
    }
    if (is_array($obj)) {
        return array_map(__FUNCTION__, $obj);
    }
    else {
        return $obj;
    }
}

If you put this function in your filter.phpfile as a helper function, then you can use it from any where in your appjust like

如果你把这个函数filter.php作为辅助函数放在你的文件中,那么你可以在你app喜欢的任何地方使用它

$users = DB::table('users')->get();
dd(stdToArray($users));

The result will be an array of arrays but Config::set('database.fetch', PDO::FETCH_ASSOC);should work and I've checked on my local server, it works just fine.

结果将是一个数组数组,但Config::set('database.fetch', PDO::FETCH_ASSOC);应该可以工作,而且我已经在我的本地服务器上检查过,它工作得很好。

Update :(Even better, to convert the array of objects to an array of arrays)

更新:(更好的是,将对象数组转换为数组数组)

$users = DB::table('users')->get();
$users = json_decode(json_encode($users), true);
dd($users); // an array of arrays

Update :Why it worked on my local server but not on OP's server, here it's : (Thanks to fideloper)

更新:为什么它在我的本地服务器上工作,而不是在OP我的服务器上,这里是:(感谢 fideloper

// I have this query at first
$users = DB::table('users')->get();

Then I've following

然后我跟着

Config::set('database.fetch', PDO::FETCH_ASSOC);
$users = DB::table('users')->get();
dd($users); // expected an array of arrays but it was objects

But, if i just remove the first db querythen it just works fine with this

但是,如果我只是删除第一个,db query那么它就可以正常工作

// $users = DB::table('users')->get();
Config::set('database.fetch', PDO::FETCH_ASSOC);
$users = DB::table('users')->get();
dd($users); // expected an array of arrays and I get it

So, it means that once you make a query and then you use Config::set(...), it doesn't change the fetch mode because the connection is already made and it's used further. So, this could be the case that, it's not working with Config::set(...);, you probably have make the connection/query. So, the solution is fideloper's answer.

因此,这意味着一旦您进行查询然后使用Config::set(...),它就不会更改获取模式,因为连接已经建立并且会被进一步使用。因此,可能是这种情况,它不适用于Config::set(...);,您可能已经建立了连接/查询。所以,解决方案是fideloper's answer

DB::connection()->setFetchMode(PDO::FETCH_ASSOC);
$users = DB::table('users')->get();
dd($users); // an array of arrays

Credit goes to fideloper.

归功于fideloper

回答by 303K

Not sure but maybe something like this works.

不确定,但也许像这样的东西有效。

$config = Config::set('database.fetch', PDO::FETCH_ASSOC);
$config->toArray();