laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.44k stars 11k forks source link

Cursor() does not support multiple sqlsvr models in iterator callback #31521

Closed TylerGauntlett closed 4 years ago

TylerGauntlett commented 4 years ago
...
'connections' => [
     'sybase' => [
          'driver' => 'sqlsrv'
          ...
     ],
],
...

Description:

When using the lazy collection cursor() method with a model using a sqlsrv connection (sybase in my case), the each() iterator will exit after a model using the same connection is called. This does not happen with a mysql based driver.

Steps To Reproduce:

Connection Setup

database.php

'connections' => [
     'sybase' => [
          'driver' => 'sqlsrv'
          ...
     ],
     'mysql' => [
          'driver' => 'mysql'
          ...
     ],
],

Working Example With mysql Driver

MysqlUser.php

<?php

namespace App\User;

use Illuminate\Database\Eloquent\Model;

class MysqlUser extends Model
{
    protected $connection = 'mysql';

    protected $table = 'users';
}

MysqlCar.php

<?php

namespace App\Car;

use Illuminate\Database\Eloquent\Model;

class MysqlCar extends Model
{
    protected $connection = 'mysql';

    protected $table = 'cars';
}

Runtime

// Prints 0, 1, 2, 3, 4
MysqlUser::take(5)->cursor()
->each(function ($user, $key) {
    MysqlCar::first();
    dump($key);
});

Failing Example With sqlsrv Driver

SybaseUser.php

<?php

namespace App\User;

use Illuminate\Database\Eloquent\Model;

class SybaseUser extends Model
{
    protected $connection = 'sybase';

    protected $table = 'users';
}

SybaseCar.php

<?php

namespace App\Car;

use Illuminate\Database\Eloquent\Model;

class SybaseCar extends Model
{
    protected $connection = 'sybase';

    protected $table = 'cars';
}

Runtime

// Only prints 0
SybaseUser::take(5)->cursor()
->each(function ($user, $key) {
    SybaseCar::first();
    dump($key);
});
driesvints commented 4 years ago

Please fill out your DB driver and version.

brizzo commented 4 years ago

I've just run into this exact issue today.