From f5ce07d78ce71c7ecc2debc75978d8c95f801a0a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 11 Nov 2021 02:49:16 +0000 Subject: [PATCH] Add index for generated id. Add checking for duplicate generated ids. #638 --- src/Entity/Customer.php | 14 +++++++++++++- src/Service/UniqueIdGenerator.php | 25 +++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 23a49082..60f9f716 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -15,7 +15,8 @@ use App\Ramcar\CustomerClassification; * @ORM\Table(name="customer", indexes={ * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}), * @ORM\Index(columns={"first_name"}, flags={"fulltext"}), - * @ORM\Index(columns={"last_name"}, flags={"fulltext"}) + * @ORM\Index(columns={"last_name"}, flags={"fulltext"}), + @ORM\Index(name="generated_id_idx", columns={"generated_id"}) * }) */ class Customer @@ -683,4 +684,15 @@ class Customer return $this->car_club_customer_hub; } + public function setGeneratedID($generated_id) + { + $this->generated_id = $generated_id; + return $this; + } + + public function getGeneratedID() + { + return $this->generated_id; + } + } diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 7822e045..3cd11f26 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -4,6 +4,8 @@ namespace App\Service; use Doctrine\ORM\EntityManagerInterface; +use App\Entity\Customer; + class UniqueIdGenerator { protected $em; @@ -15,22 +17,21 @@ class UniqueIdGenerator public function generateCustomerUniqueId($str_length) { - // retry until we get a unique id + $em = $this->em; + + // retry until we find no customer with the generated id while (true) { - try - { - } - catch (DBALException $e) - { - error_log($e->getMessage()); - // delay one second and try again - sleep(1); - continue; - } + // generate the id + $generated_id = $this->generateUniqueId($str_length); - break; + // check if generated id already exists + $cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]); + + if ($cust == null) + return $generated_id; } + } protected function generateUniqueId($str_length)