Fix checking for duplicate ids. #638
This commit is contained in:
parent
294aafc6ec
commit
f29c69cad1
1 changed files with 9 additions and 25 deletions
|
|
@ -29,7 +29,9 @@ class UniqueIdGenerator
|
|||
$generated_id = $this->generateUniqueId($str_length);
|
||||
|
||||
// check if generated id already exists
|
||||
if (!isset($this->cust_generated_ids[$generated_id]))
|
||||
$cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]);
|
||||
|
||||
if ($cust == null)
|
||||
return $generated_id;
|
||||
|
||||
sleep(1);
|
||||
|
|
@ -39,32 +41,14 @@ class UniqueIdGenerator
|
|||
|
||||
protected function generateUniqueId($str_length)
|
||||
{
|
||||
return substr(md5(time()), 0, $str_length);
|
||||
}
|
||||
$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$rand_string = '';
|
||||
$desired_length = 10;
|
||||
|
||||
protected function loadGeneratedIds()
|
||||
{
|
||||
error_log('loading generated ids ');
|
||||
$cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null');
|
||||
$cust_iter = $cust_q->iterate();
|
||||
$rand_string = substr(str_shuffle($charset), 0, $desired_length);
|
||||
|
||||
$this->cust_generated_ids = [];
|
||||
foreach ($cust_iter as $row)
|
||||
{
|
||||
$customer = $row[0];
|
||||
$generated_id = $customer->getGeneratedID();
|
||||
error_log('generated id = ' . $generated_id);
|
||||
if ($generated_id != null)
|
||||
{
|
||||
error_log('generated id is not null ' . $generated_id);
|
||||
if (!isset($this->cust_generated_ids[$generated_id]))
|
||||
{
|
||||
error_log('setting to hash ' . $generated_id);
|
||||
$this->cust_generated_ids[$generated_id] = $customer;
|
||||
}
|
||||
}
|
||||
$salt = time() . $rand_string;
|
||||
|
||||
$this->em->detach($row[0]);
|
||||
}
|
||||
return substr(md5($salt), 0, $str_length);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue