Laravel 4 - set database.fetch config at runtime

久未见 提交于 2019-12-04 07:14:26

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

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

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.

Luckily, we have access to the connection object.

Notice that the Connection object has a setFetchMode() method.

Use the Connection Object Directly

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

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

// With Eloquent model
$user = new User;
$user->getConnection()->setFetchMode(PDO::FETCH_ASSOC);
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.php file as a helper function, then you can use it from any where in your app just like

$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.

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)

// 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 query then it just works fine with this

// $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.

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

Credit goes to fideloper.

Not sure but maybe something like this works.

$config = Config::set('database.fetch', PDO::FETCH_ASSOC);
$config->toArray();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!