Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Set connection while retrieving models #18769

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ public function orWhere($column, $operator = null, $value = null)
*/
public function hydrate(array $items)
{
$instance = $this->model->newInstance();
$instance = $this->model->newInstance()->setConnection(
$this->query->getConnection()->getName()
);

return $instance->newCollection(array_map(function ($item) use ($instance) {
return $instance->newFromBuilder($item);
Expand Down Expand Up @@ -459,8 +461,7 @@ public function get($columns = ['*'])
public function getModels($columns = ['*'])
{
return $this->model->hydrate(
$this->query->get($columns)->all(),
$this->model->getConnectionName()
$this->query->get($columns)->all()
)->all();
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,10 @@ public function testGetModelsProperlyHydratesModels()
$records[] = ['name' => 'taylor', 'age' => 26];
$records[] = ['name' => 'dayle', 'age' => 28];
$builder->getQuery()->shouldReceive('get')->once()->with(['foo'])->andReturn(new BaseCollection($records));
$model = m::mock('Illuminate\Database\Eloquent\Model[getTable,getConnectionName,hydrate]');
$model = m::mock('Illuminate\Database\Eloquent\Model[getTable,hydrate]');
$model->shouldReceive('getTable')->once()->andReturn('foo_table');
$builder->setModel($model);
$model->shouldReceive('getConnectionName')->once()->andReturn('foo_connection');
$model->shouldReceive('hydrate')->once()->with($records, 'foo_connection')->andReturn(new Collection(['hydrated']));
$model->shouldReceive('hydrate')->once()->with($records)->andReturn(new Collection(['hydrated']));
$models = $builder->getModels(['foo']);

$this->assertEquals($models, ['hydrated']);
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,14 @@ public function testBelongsToManyCustomPivot()
$this->assertEquals('Jule Doe', $johnWithFriends->friends->find(4)->pivot->friend->name);
}

public function testIsAfterRetrievingTheSameModel()
{
$saved = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
$retrieved = EloquentTestUser::find(1);

$this->assertTrue($saved->is($retrieved));
}

/**
* Helpers...
*/
Expand Down