Skip to content

Commit

Permalink
Fix Eloquent find or create/update on multiple connections (#15958)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Oct 17, 2016
1 parent 778350e commit 3255dd0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ public function findOrNew($id, $columns = ['*'])
return $model;
}

return $this->model->newInstance();
return $this->model->newInstance()->setConnection(
$this->query->getConnection()->getName()
);
}

/**
Expand All @@ -238,7 +240,9 @@ public function firstOrNew(array $attributes)
return $instance;
}

return $this->model->newInstance($attributes);
return $this->model->newInstance($attributes)->setConnection(
$this->query->getConnection()->getName()
);
}

/**
Expand All @@ -254,7 +258,9 @@ public function firstOrCreate(array $attributes, array $values = [])
return $instance;
}

$instance = $this->model->newInstance($attributes + $values);
$instance = $this->model->newInstance($attributes + $values)->setConnection(
$this->query->getConnection()->getName()
);

$instance->save();

Expand Down
69 changes: 69 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,75 @@ public function testFirstOrCreate()
$this->assertEquals('Abigail Otwell', $user3->name);
}

public function testUpdateOrCreate()
{
$user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);

$user2 = EloquentTestUser::updateOrCreate(
['email' => 'taylorotwell@gmail.com'],
['name' => 'Taylor Otwell']
);

$this->assertEquals($user1->id, $user2->id);
$this->assertEquals('taylorotwell@gmail.com', $user2->email);
$this->assertEquals('Taylor Otwell', $user2->name);

$user3 = EloquentTestUser::updateOrCreate(
['email' => 'themsaid@gmail.com'],
['name' => 'Mohamed Said']
);

$this->assertEquals('Mohamed Said', $user3->name);
$this->assertEquals(EloquentTestUser::count(), 2);
}

public function testUpdateOrCreateOnDifferentConnection()
{
EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);

EloquentTestUser::on('second_connection')->updateOrCreate(
['email' => 'taylorotwell@gmail.com'],
['name' => 'Taylor Otwell']
);

EloquentTestUser::on('second_connection')->updateOrCreate(
['email' => 'themsaid@gmail.com'],
['name' => 'Mohamed Said']
);

$this->assertEquals(EloquentTestUser::count(), 1);
$this->assertEquals(EloquentTestUser::on('second_connection')->count(), 2);
}

public function testCheckAndCreateMethodsOnMultiConnections()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
EloquentTestUser::on('second_connection')->find(
EloquentTestUser::on('second_connection')->insert(['id' => 2, 'email' => 'themsaid@gmail.com'])
);

$user1 = EloquentTestUser::on('second_connection')->findOrNew(1);
$user2 = EloquentTestUser::on('second_connection')->findOrNew(2);
$this->assertFalse($user1->exists);
$this->assertTrue($user2->exists);
$this->assertEquals('second_connection', $user1->getConnectionName());
$this->assertEquals('second_connection', $user2->getConnectionName());

$user1 = EloquentTestUser::on('second_connection')->firstOrNew(['email' => 'taylorotwell@gmail.com']);
$user2 = EloquentTestUser::on('second_connection')->firstOrNew(['email' => 'themsaid@gmail.com']);
$this->assertFalse($user1->exists);
$this->assertTrue($user2->exists);
$this->assertEquals('second_connection', $user1->getConnectionName());
$this->assertEquals('second_connection', $user2->getConnectionName());

$this->assertEquals(1, EloquentTestUser::on('second_connection')->count());
$user1 = EloquentTestUser::on('second_connection')->firstOrCreate(['email' => 'taylorotwell@gmail.com']);
$user2 = EloquentTestUser::on('second_connection')->firstOrCreate(['email' => 'themsaid@gmail.com']);
$this->assertEquals('second_connection', $user1->getConnectionName());
$this->assertEquals('second_connection', $user2->getConnectionName());
$this->assertEquals(2, EloquentTestUser::on('second_connection')->count());
}

public function testPluck()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
Expand Down

0 comments on commit 3255dd0

Please sign in to comment.