mtmacdonald
mtmacdonald

Reputation: 15070

Laravel 4 - set database.fetch config at runtime

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

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

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

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.

//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);

Upvotes: 5

Views: 7192

Answers (3)

The Alpha
The Alpha

Reputation: 146201

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.

Upvotes: 3

fideloper
fideloper

Reputation: 12293

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);

Upvotes: 17

303K
303K

Reputation: 135

Not sure but maybe something like this works.

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

Upvotes: -1

Related Questions