Skip to content

Commit 7392014

Browse files
themsaidtaylorotwell
authored andcommitted
Set connection while retrieving models (#18769)
1 parent d9d99e1 commit 7392014

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ public function orWhere($column, $operator = null, $value = null)
225225
*/
226226
public function hydrate(array $items)
227227
{
228-
$instance = $this->model->newInstance();
228+
$instance = $this->model->newInstance()->setConnection(
229+
$this->query->getConnection()->getName()
230+
);
229231

230232
return $instance->newCollection(array_map(function ($item) use ($instance) {
231233
return $instance->newFromBuilder($item);
@@ -459,8 +461,7 @@ public function get($columns = ['*'])
459461
public function getModels($columns = ['*'])
460462
{
461463
return $this->model->hydrate(
462-
$this->query->get($columns)->all(),
463-
$this->model->getConnectionName()
464+
$this->query->get($columns)->all()
464465
)->all();
465466
}
466467

tests/Database/DatabaseEloquentBuilderTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,10 @@ public function testGetModelsProperlyHydratesModels()
386386
$records[] = ['name' => 'taylor', 'age' => 26];
387387
$records[] = ['name' => 'dayle', 'age' => 28];
388388
$builder->getQuery()->shouldReceive('get')->once()->with(['foo'])->andReturn(new BaseCollection($records));
389-
$model = m::mock('Illuminate\Database\Eloquent\Model[getTable,getConnectionName,hydrate]');
389+
$model = m::mock('Illuminate\Database\Eloquent\Model[getTable,hydrate]');
390390
$model->shouldReceive('getTable')->once()->andReturn('foo_table');
391391
$builder->setModel($model);
392-
$model->shouldReceive('getConnectionName')->once()->andReturn('foo_connection');
393-
$model->shouldReceive('hydrate')->once()->with($records, 'foo_connection')->andReturn(new Collection(['hydrated']));
392+
$model->shouldReceive('hydrate')->once()->with($records)->andReturn(new Collection(['hydrated']));
394393
$models = $builder->getModels(['foo']);
395394

396395
$this->assertEquals($models, ['hydrated']);

tests/Database/DatabaseEloquentIntegrationTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,14 @@ public function testBelongsToManyCustomPivot()
10611061
$this->assertEquals('Jule Doe', $johnWithFriends->friends->find(4)->pivot->friend->name);
10621062
}
10631063

1064+
public function testIsAfterRetrievingTheSameModel()
1065+
{
1066+
$saved = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
1067+
$retrieved = EloquentTestUser::find(1);
1068+
1069+
$this->assertTrue($saved->is($retrieved));
1070+
}
1071+
10641072
/**
10651073
* Helpers...
10661074
*/

0 commit comments

Comments
 (0)