generate unique serial numbers for shop order the doctrine way generate unique serial numbers for shop order the doctrine way symfony symfony

generate unique serial numbers for shop order the doctrine way


It is considered as a drawback in e-commerce when auto increment is used for order numbers. The reason is that your competition can easily track these numbers and more or less know, how many orders you had for a period of time. They can use this knowledge to their benefit. That is why there was a request in our company to generate random order numbers. We did this by "scrambling" the ID retrieved from auto increment to generate a 6 character identifier containing numbers and letters (when you use letters, you can easily fit more then million orders to 6 chars, payment gateways should be OK with letters). This was done in a deterministic way, so this number can be converted back to the ID (although this is not strictly necessary).

Your solution with a separate table for order numbers seems pretty fine to me. You can also have a unique constraint on orderNumber and generate a random number or hash of a random number(seed should reflect the actual time and users session id, to limit probability of collisions) and try to update the entity. If it failed you will try to regenerate. You should then set a maximum number of times this can fail, so it would not cycle forever, if there is a different problem.


Has described in this presentation Doctrine ORM Good Practices and Tricks

I want to suggest you to avoid auto-generated identifiers and so to use an UUID instead with, as example the ramsey/uuid Library.

So, As Example, Your Order entity can be some similar to

 ....../** * @ORM\Id * @Column(type="string") * @GeneratedValue(strategy="NONE") */protected $id = null;......public function __construct(){    $this->id = Uuid::uuid4();}

So this is accessible before you persist it on the database.

Better inspiration on the (Great!) talk of Pivetta

Hope this help