bentools / doctrine-ulid
ULID (Universally Unique Lexicographically Sortable Identifier) support for Doctrine IDs
Installs: 4 551
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.3
- robinvdvleuten/ulid: ~5.0
Requires (Dev)
- doctrine/orm: ^2.3
- matthiasnoback/doctrine-orm-test-service-provider: ^3.0
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-10-25 02:34:34 UTC
README
Doctrine ULID generator
This small library adds support for ULID in Doctrine.
ULIDs act like UUIDs that can be lexicographically sorted. ULIDs also have a smaller footprint (26 ANSI characters vs. 36 for UUIDs);
This package integrates robinvdvleuten/ulid as a CustomIdGenerator
.
Installation
composer require bentools/doctrine-ulid
Usage
Use the provided class as a custom ID generator:
use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class Foo { /** * @var string * * @ORM\Id() * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class="\BenTools\ULIDGenerator") * @ORM\Column(type="string", length=26) */ private $id; // ... }
Or use the following trait:
use Doctrine\ORM\Mapping as ORM; use BenTools\GeneratedULIDTrait; /** * @ORM\Entity() */ class Foo { use GeneratedULIDTrait; // ... }
If you want to set ULIDs by yourself (this way, they can be generated on the client side), use the EditableULIDTrait
which will expose a setId()
method:
use Doctrine\ORM\Mapping as ORM; use BenTools\EditableULIDTrait; /** * @ORM\Entity() * @ORM\HasLifecycleCallbacks() */ class Foo { use EditableULIDTrait; // ... }
- If
setId()
is not called, an ULID will be automatically generated on persist. - Don't forget to add a
@HasLifecycleCallbacks()
annotation on top of your entity for this behavior to work properly.
Tests
./vendor/bin/phpunit
License
MIT.