Fix checking for duplicate ids. #638

This commit is contained in:
Korina Cordero 2021-11-16 04:45:19 +00:00
parent 294aafc6ec
commit f29c69cad1

View file

@ -29,7 +29,9 @@ class UniqueIdGenerator
$generated_id = $this->generateUniqueId($str_length); $generated_id = $this->generateUniqueId($str_length);
// check if generated id already exists // 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; return $generated_id;
sleep(1); sleep(1);
@ -39,32 +41,14 @@ class UniqueIdGenerator
protected function generateUniqueId($str_length) protected function generateUniqueId($str_length)
{ {
return substr(md5(time()), 0, $str_length); $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
} $rand_string = '';
$desired_length = 10;
protected function loadGeneratedIds() $rand_string = substr(str_shuffle($charset), 0, $desired_length);
{
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();
$this->cust_generated_ids = []; $salt = time() . $rand_string;
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;
}
}
$this->em->detach($row[0]); return substr(md5($salt), 0, $str_length);
}
} }
} }