Best way to create a GUID on a dataobject?

Silverstripe Version: 4.2.1
PHP version: Local: MAMP 7.0.8. Remote: Upto 7.2.4 (no SSH access)

Background:

I have a project that is using a Jquery calendar and updating dataobjects when dragging events via AJAX.

Page is restricted by login.

But I don’t want to pass a ID to update each event. Would rather pass a GUID to be safe.

Question:
What is the easiest way to generate a GUID or UUID?

Found this:
https://addons.silverstripe.org/add-ons/lekoala/silverstripe-uuid

But having issues installing that due to requirements. Have a old mac 10.9.5 and mamp 3.5.2, and yes I need to upgrade, but that is for anther day.

Is there a simple way of generating a unique ID?

Thanks
Regards
Scott

Do you really need a full fledged Uuid or would a unique hash/string do it?

I mean, you could just create a MD5 hash with something random as salt/content.
(I know md5 isnt secure but do you really need security in this case?)
I the end you just need non guessable identifiers.

Hi Ironcheese,

Had never thought of that. Excellent idea and perfect solution. Will do something like this for benefit of other searchers:

private static $db = [
‘Hash’ => ‘Varchar(40)’ // cryptographic hash identification to this session
];

$this->Hash = sha1($this->ID . ‘-’ . microtime());

Really just to stop someone guessing the next ID, but they would still need to be logged in.

Thanks :slight_smile:

Glad to hear that.
On another thought though: You could use a “Hash ID” Package as well.

https://packagist.org/packages/hashids/hashids

I am using this package myself in a SilverStripe 3.6 installation for a Url Shortener setup.
This might provide you with “nicer” looking hashes than 40 char long monstrosities :slight_smile:
…and: you wouldn’t need a extra DB field

1 Like

Thanks for the suggestion. Handy looking package!