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\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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue