Generating UUID with cakephp

Most of the Database implementations use auto increment integers as primary keys, but in some cases we might need a random string like UUID as primary key. UUID is a 32 byte string separated by four hyphens, for a total of 36 characters. For example: 550e8400-e29b-41d4-a716-446655440000 One of the major advantages of using UUID is that it can be generated in parallel without communication between clustered DB servers. These are unique, not only within a single table, but also across tables and databases. With this, I hope you might have got some idea about UUID, let us now talk about how to implement it in our database in cake PhP projects. In Cake php’s uuid implementation is based on RFC 4122 standard. It is basically splitted as:
  • time_low
  • time_mid
  • PHP process ID
  • node
Time_low and Time_mid constitute the Timestamp value which is of 60 bit . The node field consists of an IEEE 802 MAC address, usually the host address or the host name. Implementation: It is very easy all you need is to specify your primary key as a CHAR (36) or BINARY (36) and CakePhP will automatically generate them for you, when new records are created. Or alternatively you can use the String class which includes convenience methods for creating and manipulating strings and is normally accessed statically. For example: – $uuid = String::uuid (); It will assign a unique id to $uuid which you can use in your project.