We built a shop system with symfony2 using doctrine which has to generate unique serial order numbers.
There's an Order entity which originally had a property orderNumber
The Order entity is our "shopping session", so not only submitted orders are persisted but every time a user puts sth. into his cart. The orderNumber is default null and should only be set when the user submits an order.
In plain MySQL I would have done sth. like this:
INSERT INTO orders (orderNumber, [...])
SELECT MAX(orderNumber) + 1, [...] FROM orders;
But to achieve this in doctrine I would have to do a table write lock, select the max orderNumber, set it to order entity, persist and then unlock the table. I don't wanted to do that (cause of the table lock) so the quick workaround was to kick the orderNumber property and just show the id as order number. But that's not a nice solution, the order numbers have gaps and getting higher and higher (after 4 months we have an auto increment value of 140k and the order number should not have more than 6 chars, so we need a solution before running into this limit).
To create a mapped superclass and split the Order entity into separate entities and tables for temporary and submitted orders would lead to too much code changes. So the next idea is to create a separate OrderNumber entity with an auto increment field and just set this to the Order entity when the user submits the order. This seems to be a way to do it without much changes.
But maybe there's a better way to solve this, what's the best solution to do it the doctrine way?