EllisLab text mark
Advanced Search
     
Using DMZ 1.7.1 (DataMapper OverZealous Edition) with UUID’s
Posted: 24 September 2010 04:27 PM   [ Ignore ]
Avatar
Joined: 2010-05-16
16 posts

One serious limitation of DMZ appears to be its insistence on only allowing integer ID’s. This is nonsense IMNSOHO. For starters, it’s considered really bad practice these days to let the DB generate your id’s. raspberry

I created a UUID work-around that so far appears to be working pretty well. It may need some tweaking later on, but the good folks at OverZealous should add this naively as soon as possible. A UUID/GUID generator would be a nice addition also. wink

Open the datamapper.php file within the libraries folder.

1. Comment out line 581 and add line 582:

// 'rules' => array('integer')
'rules' => array(''


2. Edit lines 738 through 743 as follows:

// if( ! empty($id) && is_numeric($id))
if( ! empty($id) )
{
    
// $this->get_by_id(intval($id));
    
$this->get_by_id($id);

3. Comment out line 5939

// $item->{$field} = intval($item->{$field}); 

UPDATE

4. Add the following code to support the insertions of UUIDrelationships:

Lines 4846, 4854 and 4875

$data['id'UUID::v4(); // use whatever version of UUID you prefer
$this->db->insert($relationship_table$data); // existing insert statement 

Again, there may be additional intval conversions that I missed in some other files or validation, but you get the picture.

If you are using MySQL, make your id field a CHAR(36) that does not auto-increment.

When saving your objects, do this:

$org = new Org();

// You will probably want to write a class to generate UUID's
$org->id '7643D300-EEE2-4554-9927-71935BA436A6';

$org->orgname "My Organization Name";

// need to call this because we're using our own UUID
$org->save_as_new(); 

To retrieve the record, do this:

$org = new Org();
$org->where('id''7643D300-EEE2-4554-9927-71935BA436A6')->get(); 

This seems to work well, but if you find any problems that need to be addressed further, feel free to comment them here!  smile

Cheers!

-Beau, WebTigers

 
Posted: 24 September 2010 04:37 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-16
16 posts

Also, here’s a neat little class for generating and validating UUID’s and GUID’s. Drop this file into your application’s /helpers folder. I autoload it because I always use it. (See attached download file.)

$autoload['helper'= array('UUID'); 

Cheers!

 
Posted: 24 September 2010 05:24 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-15
405 posts

As I understood, it will allow MongoDB IDs?

 
Posted: 24 September 2010 09:32 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-16
16 posts
Sbioko - 24 September 2010 09:24 PM

As I understood, it will allow MongoDB IDs?

It should work just fine for MongoDB ID’s. 

-Beau

 
Posted: 25 September 2010 02:18 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-15
405 posts

Well, thanks! Great contribution!

UPD: I was wrong. It is not for me, anyway thanks!

 
Posted: 25 September 2010 06:11 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

@WebTigers,

Care to elaborate on why you think integer ID’s are a bad idea, and UUID’s are so much better?

For me, ID’s are just internal keys, used to define the relations between tables. From that point of view, integers are the best choice, they allow for easy indexing and fast retrieval. Indexing and querying a char(36) field is a slower, and creates bigger indexes.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 25 September 2010 10:55 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-16
16 posts
WanWizard - 25 September 2010 10:11 AM

@WebTigers,

Care to elaborate on why you think integer ID’s are a bad idea, and UUID’s are so much better?

For me, ID’s are just internal keys, used to define the relations between tables. From that point of view, integers are the best choice, they allow for easy indexing and fast retrieval. Indexing and querying a char(36) field is a slower, and creates bigger indexes.

Integers are not always the best choice, which is why UUID’s came into being in the first place.

In the world of small procedural script “garage applications”, it probably doesn’t matter much, if at all, what you use. These apps are so small you’re not using enough resources one way or the other for it to make a difference. A UUID vs. Integer search on 10K records will happen in an instant and the larger sized key field does not consume a noticeable amount of storage on the drive. These apps are typically so small that no one will notice or care if you’re using UUID’s or Intergers for keys.

However, once you get into more OO enterprise-class architecture, even with smaller applications that may one day grow into major applications later on, using enterprise-class foundations, which UUID’s (or GUID’s) play a role, will save you a lot of headaches later on.

There are some really great benefits to using UUID’s over Integers with objects, such as every key is unique even between objects (ie. records).

Do a search for UUID vs. Integer keys and you notice a broad range of opinion on the subject. However, most of the naysayers are those people who are not running enterprise-class RDBMS like Oracle or DB2 or even MSSQL nor are they writing enterprise-class apps that really use the benefits of UUID’s.

For me, the bottom line is I usually work with really large enterprises and as such, even though I may be building a smaller internal app here and there, I always try to build them using the same enterprise-class structures we use in the global applications, even when using smaller frameworks like CI.

It all depends on the flavor of the Kool-aid you choose to drink.  wink

 
Posted: 27 September 2010 05:57 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-02-14
59 posts

I dont want to be rude, my english is not perfect.

You wrote a long reply but in the end you gave no advantage of using UUID over using interger for keys. To sum up, you said “UUID are for big players running expensive software” and “integers for small project”. That’s a bit “lame”.

Anyway, thanks for your contribution to DMZ. I am sure people sharing your need will concider it usefull.

 
Posted: 27 September 2010 06:49 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-16
16 posts
jpi - 27 September 2010 09:57 PM

I dont want to be rude, my english is not perfect.

You wrote a long reply but in the end you gave no advantage of using UUID over using interger for keys. To sum up, you said “UUID are for big players running expensive software” and “integers for small project”. That’s a bit “lame”.

Anyway, thanks for your contribution to DMZ. I am sure people sharing your need will concider it usefull.

LOL! Yes, it could be considered a lame response, but it really does depend on the application you’re building. Professionally, I like to know the id of the object I’m persisting BEFORE it gets inserted into the DB. More control, better results, less headaches. But you’re right. For smaller garage applications with way less than 1 million records, it really is a matter of preference.  wink