castt's avatar

Lumen PDO Fetch Style

How would I switch to PDO::FETCH_ASSOC in Lumen for mysql database? In Laravel I used be able to change it in config/database.php under 'fetch' parameter.

0 likes
20 replies
bashy's avatar

You want the toArray()?

Add this on the end to a query....

->toArray();

// like
User::where('job', 'teaboy')->toArray();
castt's avatar

@bashy What if I am using Query Builder ?

DB::table('users')->get();
bashy's avatar

Yeah won't work on that but why are you?

castt's avatar

@bashy why would I use Query Builder Instead? Because while building complex architecture sometimes you want to go beyond Eloquent and build way more complex queries (using deferred selects for instance).

bashy's avatar

And the need for an array? Build a helper function to json_decode() it?

castt's avatar

@bashy performance issue. What's the point of iterating through each record and changing format of each record using PHP, when you can get it out of the box with PDO? O(1) vs O(n), right?

bashy's avatar

Well :boom:

I was just giving options since you didn't know how to use the toArray()

castt's avatar

@bashy thanks man, but I knew how to use toArray() which isn't the solution I am looking for. I'm not looking for 'workaround', I need to know if it's still possible in lumen to change PDO connection type. I guess, I wasn't clear on that.

toniperic's avatar

Why? Why not work with objects?

You can do

DB::setFetchMode(PDO::FETCH_ASSOC)->table('users')->get();

but that's set by default, so not sure what you're really trying to achieve here.

castt's avatar

@toniperic 1. I don't think your code will work. Something like this will (assuming singleton):

DB::setFetchMode(\PDO::FETCH_ASSOC);
DB::table('users')->get();

And it will actually give the result I am looking for (and it's not enabled by default). You can test the same code without setFetchMode line in Lumen and you will get array of objects instead.

  1. Don't you find it kind of redundant to use setFetchMode in every query I need associative arrays? I would prefer to have single entry point for that kind of setting for my database connection as it was in Laravel.
castt's avatar

I guess that would make handy Pull Request feature to put this setting somewhere in .env or under bootstrap (since we don't have config files anymore). Does anyone else interested in it?

1 like
castt's avatar

@toniperic DB facade will actually use this setting instead: vendor\laravel\lumen-framework\config\database.php 'fetch' => PDO::FETCH_CLASS,

toniperic's avatar
Level 30

Yeah sorry, a bit sleepy so I've missed that call to setFetchMode doesn't return the instance of the same object, but you got the point.

Just wanted to make sure it's on by default and you're right, apparently it got changed from PDO::FETCH_CLASS to PDO::FETCH_OBJ like a month ago. You can see it in the commits history if you like.

Anyways, as for your 2nd question of finding it redundant - I'd agree. There is a simple workaround for this and no need to make a pull request as this is something that's already been taken care of.

Create a config directory within your project root, and copy into it the vendor/laravel/lumen-framework/config/database.php file.

Then just edit the file you just copied , which is your/project/root/config/database.php:

'fetch' => PDO::FETCH_CLASS,

to whatever you like.

Hope I could help.

castt's avatar

@toniperic thanks man! I completely forgot that I can still use config directory. That solved the problem.

Please or to participate in this conversation.