Skip to content
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
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use Exception;
use Throwable;
use ArrayAccess;
use Carbon\Carbon;
use LogicException;
Expand Down Expand Up @@ -1521,6 +1522,38 @@ public function save(array $options = [])
return $saved;
}

/**
* Save the model to the database using transaction.
*
* @param array $options
* @return bool
*
* @throws \Throwable
*/
public function saveOrFail(array $options = [])
{
$db = $this->getConnection();
$result = false;

$db->beginTransaction();

try {
$result = $this->save($options);

$db->commit();
} catch (Exception $e) {
$db->rollBack();

throw $e;
} catch (Throwable $e) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for all this, just use finally

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, wait, ignore me - we don't want to always rollback, lol

$db->rollBack();

throw $e;
}

return $result;
}

/**
* Finish processing on a successful save operation.
*
Expand Down
63 changes: 63 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,34 @@ public function testEmptyMorphToRelationship()
$this->assertNull($photo->imageable);
}

public function testSaveOrFail()
{
$date = '1970-01-01';
$post = new EloquentTestPost([
'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
]);

$this->assertTrue($post->saveOrFail());
$this->assertEquals(1, EloquentTestPost::count());
}

/**
* @expectedException Exception
*/
public function testSaveOrFailWithDuplicatedEntry()
{
$date = '1970-01-01';
EloquentTestPost::create([
'id' => 1, 'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
]);

$post = new EloquentTestPost([
'id' => 1, 'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
]);

$post->saveOrFail();
}

public function testMultiInsertsWithDifferentValues()
{
$date = '1970-01-01';
Expand Down Expand Up @@ -473,6 +501,41 @@ public function testNestedTransactions()
});
}

public function testNestedTransactionsUsingSaveOrFailWillSucceed()
{
$user = EloquentTestUser::create(['email' => '[email protected]']);
$this->connection()->transaction(function () use ($user) {
try {
$user->email = '[email protected]';
$user->saveOrFail();
} catch (Exception $e) {
// ignore the exception
}

$user = EloquentTestUser::first();
$this->assertEquals('[email protected]', $user->email);
$this->assertEquals(1, $user->id);
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests follow testNestedTransactions() with the only different with using saveOrFail().

}

public function testNestedTransactionsUsingSaveOrFailWillFails()
{
$user = EloquentTestUser::create(['email' => '[email protected]']);
$this->connection()->transaction(function () use ($user) {
try {
$user->id = 'invalid';
$user->email = '[email protected]';
$user->saveOrFail();
} catch (Exception $e) {
// ignore the exception
}

$user = EloquentTestUser::first();
$this->assertEquals('[email protected]', $user->email);
$this->assertEquals(1, $user->id);
});
}

public function testToArrayIncludesDefaultFormattedTimestamps()
{
$model = new EloquentTestUser;
Expand Down