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.1] Add Model::saveOrFail() to execute save operation within database transaction. #11202

Merged
merged 2 commits into from
Dec 7, 2015

Conversation

crynobone
Copy link
Member

This would simplify the following operation:

try {
    DB::transaction(function () use ($device) {
        $device->save();
    });
} catch (Exception $e) {
    return false;
}

to

try {
    $device->saveOrFail();
} catch (Exception $e) {
    return false;
}

Signed-off-by: crynobone [email protected]

$saved = false;

$this->getConnection()->transaction(function () {
$saved = $this->save($options);
Copy link
Member

Choose a reason for hiding this comment

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

this function will not modify the saved variable - php puts it in a different scope

@GrahamCampbell
Copy link
Member

Needs tests because the current implementation doesn't actually work.

@crynobone crynobone force-pushed the feature/save-or-fail branch from 06fcda3 to 3496a5a Compare December 5, 2015 11:57
$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().

@crynobone
Copy link
Member Author

Needs tests because the current implementation doesn't actually work.

Duplicate Illuminate\Database\Connection::transaction(), rebase and add tests.

$db->rollBack();

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

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
Member

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

Signed-off-by: crynobone <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants