Issue with same order number generation

32 Views Asked by At

My application is written in symfony, which has an online checkout system. Generally when two or more user at the same time went through the checkout, it generates same order number. As a consequence, it is generating redundancy in the database which may result into inconsistency.

We are generating nine digits uniqid for order number and at the same time we are checking the newly generated #id against the database to avoid duplication.

$slug = substr(hexdec(uniqid('', false)),0,9);
        while($this->_doctrine->getManager()->getRepository('ZACartBundle:Cart')->findOneBySlug($slug) != null){
            $slug = substr(hexdec(uniqid('', false)),0,9);
        }
        return $slug;

Basically, we need to have the unique slug for the order number while concurrent users going through the checkout process.

1

There are 1 best solutions below

0
AudioBubble On

Your script generate the unique slug from the timestamp, but the number too big (16 digits) and you only take the first 10 digits, that's why the slug is identical when more than 1 users have the same id when accessing the cart in short period of time.

you can consider getting the last 10 digits to fix this issue:

$slug = substr(hexdec(uniqid('', false)), -10);