Add index for generated id. Add checking for duplicate generated ids. #638
This commit is contained in:
parent
f828db422f
commit
f5ce07d78c
2 changed files with 26 additions and 13 deletions
|
|
@ -15,7 +15,8 @@ use App\Ramcar\CustomerClassification;
|
||||||
* @ORM\Table(name="customer", indexes={
|
* @ORM\Table(name="customer", indexes={
|
||||||
* @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}),
|
* @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}),
|
||||||
* @ORM\Index(columns={"first_name"}, flags={"fulltext"}),
|
* @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
|
class Customer
|
||||||
|
|
@ -683,4 +684,15 @@ class Customer
|
||||||
return $this->car_club_customer_hub;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ namespace App\Service;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
use App\Entity\Customer;
|
||||||
|
|
||||||
class UniqueIdGenerator
|
class UniqueIdGenerator
|
||||||
{
|
{
|
||||||
protected $em;
|
protected $em;
|
||||||
|
|
@ -15,22 +17,21 @@ class UniqueIdGenerator
|
||||||
|
|
||||||
public function generateCustomerUniqueId($str_length)
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
// generate the id
|
||||||
{
|
$generated_id = $this->generateUniqueId($str_length);
|
||||||
}
|
|
||||||
catch (DBALException $e)
|
// check if generated id already exists
|
||||||
{
|
$cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]);
|
||||||
error_log($e->getMessage());
|
|
||||||
// delay one second and try again
|
if ($cust == null)
|
||||||
sleep(1);
|
return $generated_id;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateUniqueId($str_length)
|
protected function generateUniqueId($str_length)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue