Framework: Eloquent 5.4.0 removes/breaks $connection->setFetchMode()

Created on 2 Feb 2017  路  5Comments  路  Source: laravel/framework

  • Laravel Version: N/A (using Eloquent without Laravel)
  • Eloquent Version: 5.4.0
  • PHP Version: 7.0

Description:

In Eloquent 5.3.23 the following code worked:

    $fetchMode = \PDO::FETCH_ASSOC;
    $sql = "SELECT * FROM some_table";
    $dbConn = \Illuminate\Database\Capsule\Manager::connection();
    $dbConn->setFetchMode ($fetchMode);
    $data = $dbConn->select ($sql);

Steps To Reproduce:

In Eloquent version 5.4.0 the above code produces the following error:

Call to undefined method Illuminate\Database\MySqlConnection::setFetchMode()

I've looked at the code and it's not quite clear to me how to achieve the previously working/supported behavior using version 5.4.0.

Most helpful comment

Not sure how to fix it without using (more) laravel components tho.

If this is impossible (or even quite difficult) to do without additional Laravel components, then I would humbly suggest that this is not a good change. Not everybody (myself included) uses Eloquent with Laravel and the ability to easily integrate with non-Laravel codebases is an important factor for a framework like Eloquent.

All 5 comments

Hi,

This is documented in the Laravel 5.4 upgrade guide (Search for "Fetch Mode").

The ability to customize the fetch mode is removed and is defaulted to PDO::FETCH_OBJ. You can override this by using an event listener, as stated in the upgrade guide:

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(...);
});

Not sure how to fix it without using (more) laravel components tho.

Also see https://github.com/laravel/framework/issues/17557

Not sure how to fix it without using (more) laravel components tho.

If this is impossible (or even quite difficult) to do without additional Laravel components, then I would humbly suggest that this is not a good change. Not everybody (myself included) uses Eloquent with Laravel and the ability to easily integrate with non-Laravel codebases is an important factor for a framework like Eloquent.

Well, the only additional component you really would need would be the event listener, but I agree it's not the most elegant solution. But the laravel team probably has a good reason to remove the option.

Yeah, I've adapted my code to convert the resulting object to an array manually. Not as elegant but it works. Thanks :-)

wow, closed issue, booo!

Was this page helpful?
0 / 5 - 0 ratings