From 67b74b2a34112ea1adf1c72674f870c14c0d5658 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Feb 2016 11:26:21 +1100 Subject: [PATCH] Table locking for public_id integrity --- app/Models/EntityModel.php | 1 + app/Ninja/Repositories/ClientRepository.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index b8e7d651ada4..9483f84a8c6a 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -27,6 +27,7 @@ class EntityModel extends Eloquent $lastEntity = $className::withTrashed() ->scope(false, $entity->account_id) ->orderBy('public_id', 'DESC') + ->lockForUpdate() ->first(); if ($lastEntity) { diff --git a/app/Ninja/Repositories/ClientRepository.php b/app/Ninja/Repositories/ClientRepository.php index 3e43d8f34f5f..ab9789f1cae9 100644 --- a/app/Ninja/Repositories/ClientRepository.php +++ b/app/Ninja/Repositories/ClientRepository.php @@ -66,6 +66,8 @@ class ClientRepository extends BaseRepository { $publicId = isset($data['public_id']) ? $data['public_id'] : false; + DB::beginTransaction(); + if (!$publicId || $publicId == '-1') { $client = Client::createNew(); } else { @@ -75,6 +77,9 @@ class ClientRepository extends BaseRepository $client->fill($data); $client->save(); + DB::commit(); + + /* if ( ! isset($data['contact']) && ! isset($data['contacts'])) { return $client;