UUID component for CakePHP

Published on and tagged with cakephp  component

I just uploaded a simple component to generate UUIDs (Universally Unique Identifiers) to the downloads section. It is simply a wrapper for a function found in the comments on php.net.

To use the component add it to the components array in your controller and generate a new UUID in the following way:

$this->Uuid->generate();

14 comments baked

  • rikman

    Nice.

    Just yesterday I built my own Component for generating UUIDs … with the same code from PHP.net ;-)

  • jgonzalez

    could you provide an example of using the uuid component to auto generate the pk’s in a model object ?

    Thank you !

  • cakebaker

    @jgonzalez: Hm, the usual way to auto generate PKs is to use an auto-increment column in your table. I don’t know how you would use UUIDs as PKs as I always use the default way.

  • PD

    Thank you. This is great. Using it on a site.

  • cakebaker

    @PD: Cool to hear that :)

  • cakebaker » Generating UUIDs with CakePHP

    […] months ago I presented in “UUID component for CakePHP” a simple component to generate UUIDs (Universally Unique Identifiers). With changeset 5552, this […]

  • Thomas Wright

    You, my friend, are a lifesaver.
    Just what I wanted – actually for PKs like the chap above.
    To solve his problem I just whacked in the following line before the save call:
    $this->data[‘Record’][‘id’] = $this->Uuid->generate();
    And, for reference, UUIDs are useful as PKs when you’re sharding a database.
    Thanks again,
    Tom

  • cakebaker

    @Tom: You are welcome!

    If you defined your id field as CHAR(36), cake will automatically insert a UUID, so it is not really necessary to use this component for this purpose ;-)

  • Thomas Wright

    Yeah, I read that and got really excited about it.
    Unfortunately I couldn’t coax it into action and found it easier to implement your component.
    I guess my code would be neater if I could rely on the inbuilt UUIDing – any gotchas I should know about?

  • cakebaker

    @Tom: At least in theory it should work automagically, but there have been some issues with it in the past…

    Personally, I don’t have used such PKs yet, so I have no experience with them.

  • HolisticNetworking

    Oh, I’ll be paying attention to this site in the future. Yes, I will!

    Thanks for the awesome component. I’m a noob to the CakePHP world, but I’m digging it. The only problem: I need to create UUID’s for objects in my database because I’m sharing a foreign key among a couple different tables, yet the UUID thing that’s supposed to work automatically in Cake doesn’t seem to want to work for me.

  • cakebaker

    @HolisticNetworking: Thanks for your comment!

    Well, in the meantime this component is no longer necessary, you can simply use:
    String::uuid();

    So far I don’t have used the automagic UUID functionality, but maybe this section from the cookbook helps: http://book.cakephp.org/view/70/Using-UUIDs-as-Primary-Keys

  • Eduardo

    when using UUID’s as PKs in your table, is there a way to get the UUID of the last insert. What I’m actually getting is an auto_increment value eventhough the table does not have an auto_increment id as PK.

    See below…

    $return = $db->save($insert);
    			if($return){
    				$dataSource->commit($db);
    				return $db->id;
    			}
  • cakebaker

    @Eduardo: Hm, which CakePHP version do you use?

    I just created a table with UUIDs as PKs and it works as expected: after saving a record the id property of my model contains the UUID created by CakePHP (v1.3.11).

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License