Harold Anthony Ocampo Harold Anthony Ocampo - 25 days ago 7
PHP Question

Doctrine Annotations type="guid" and strategy="UUID" Behavior?

Ok, so I just want to know what the behavior of this annotation does:

* @ORM\Id
* @ORM\Column(name="id", type="guid")
* @ORM\GeneratedValue(strategy="UUID")
protected $id;

Are the keys in an incrementing order as I insert rows to my table?

I do know that the keys are not as simple as
and it behaves like

  • 007f13ff-ce26-11e4-8e3d-a0b3cce9bb7e

  • 00805a63-ce26-11e4-8e3d-a0b3cce9bb7e

  • 0b1b6ca9-d178-11e4-8e3d-a0b3cce9bb7e

but my question is, are the keys in a specific order like the first inserted row is a lower alphanumeric value than the last value.. Always?

Answer Source

Short answer: UUID are not in a specific order.

Some details

UUID are an alternative to autoincrement integers which are becoming more and more used because they have some advantages.

  1. You will not break your application when you insert the 65537 record (or similar 2^x + 1, depending on the original size of the ID). Worst that can happen is that you have a duplicate identifier, but that's very unlikey: https://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates

  2. You can generate them in your code before storing the record; you don't have to get "last_insert_id" from the database. That's good, for example when you also have to store some related records in a different table (of course Doctrine may handle this for you but this is another story).

  3. You don't give away the number of records in your table to a smart guy who can read the ID of the current order :-)