The cycle/entity-behavior-uuid
package provides the ability to use ramsey/uuid
as a Cycle ORM property type.
Install this package as a dependency using Composer.
composer require cycle/entity-behavior-uuid
The package provides several version of UUID you can use for entities.
A version 1 UUID uses the current time, along with the MAC address (or node) for a network interface on the local machine.
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid1;
use Ramsey\Uuid\UuidInterface;
#[Entity]
#[Uuid1(field: 'uuid', node: '00000fffffff', clockSeq: 0xffff, nullable: false)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
UUID v2 uses the current time, along with the MAC address (or node) for a network interface on the local machine. Additionally, a version 2 UUID replaces the low part of the time field with a local identifier such as the user ID or group ID of the local account that created the UUID.
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid2;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
#[Entity]
#[Uuid2(
field: 'uuid',
localDomain: Uuid::DCE_DOMAIN_PERSON,
localIdentifier: '12345678',
node: '00000fffffff',
clockSeq: 0xffff,
nullable: false
)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
Uses a version 3 (name-based) UUID based on the MD5 hash of a namespace ID and a name
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid3;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
#[Entity]
#[Uuid3(field: 'uuid', namespace: Uuid::NAMESPACE_URL, name: 'https://example.com/foo', nullable: false)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
They are randomly-generated and do not contain any information about the time they are created or the machine that generated them.
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid4;
use Ramsey\Uuid\UuidInterface;
#[Entity]
#[Uuid4(nullable: false)]
class User
{
#[Column(field: 'uuid', type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
Uses a version 5 (name-based) UUID based on the SHA-1 hash of a namespace ID and a name
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid5;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
#[Entity]
#[Uuid5(field: 'uuid', namespace: Uuid::NAMESPACE_URL, name: 'https://example.com/foo', nullable: false)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
Ordered-Time Uses a version 6 (ordered-time) UUID from a host ID, sequence number, and the current time
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid6;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
#[Entity]
#[Uuid6(field: 'uuid', node: '00000fffffff', clockSeq: 0xffff, nullable: false)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
Version 7 UUIDs solve two problems that have long existed with the use of version 1 UUIDs:
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid7;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
#[Entity]
#[Uuid7(field: 'uuid', nullable: false)]
class User
{
#[Column(type: 'uuid', primary: true)]
private UuidInterface $uuid;
}
In all UUID attributes, there is a nullable
parameter. If you set this parameter to true, it will disable automatic
UUID generation. In this case, your database field must be nullable, or you must generate a value for the field yourself.
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Entity\Behavior\Uuid\Uuid4;
use Ramsey\Uuid\UuidInterface;
#[Entity]
#[Uuid4(field: 'token', nullable: true)]
class User
{
#[Column(type: 'primary')]
private int $id;
#[Column(type: 'uuid', nullable: true)]
private ?UuidInterface $token = null
}
Warning
If you have a customuuid
column declaration, it should be compatible withBehavior\Uuid\Uuid*
column type, otherwise an exception will be thrown.