jeancodogno / doctrine-snowflake-id-bundle
Symfony bundle to automatically generate Snowflake IDs
Installs: 58
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/jeancodogno/doctrine-snowflake-id-bundle
Requires
- php: ^8.2
- doctrine/doctrine-bundle: ^2.6
- doctrine/mongodb-odm-bundle: ^5.3
- doctrine/orm: ^3.3
- godruoyi/php-snowflake: ^3.1
- symfony/framework-bundle: ^7.2
Requires (Dev)
- mockery/mockery: ^1.6
- nunomaduro/phpinsights: ^2.13
- pestphp/pest: ^3.8
- pestphp/pest-plugin-arch: ^3.1
- pestphp/pest-plugin-type-coverage: ^3.5
- phpstan/phpstan: ^2.1
This package is auto-updated.
Last update: 2025-10-16 02:58:50 UTC
README
Symfony bundle to automatically assign Snowflake-based IDs to your Doctrine entities and documents. Supports both primary keys and any other custom fields using attributes.
π Features
β Assigns unique Snowflake IDs to your entities
π Works for both primary keys and custom field using #[SnowflakeColumn] or #[SnowflakeField]
π§© Seamlessly integrates with Doctrine ORM and Doctrine ODM
π§ͺ Fully testable
π¦ Installation
Install via Composer:
composer require jeancodogno/doctrine-snowflake-id-bundle
The bundle uses autoconfiguration, no need to manually register it in
bundles.php.
π‘ Usage
PHP does not have a native type that supports big integers, so the variable must be defined as a
string.
π (Doctrine ORM) Using Snowflake ID Generator
Use the SnowflakeIdGenerator class with Doctrine ORMβs custom ID generation:
use JeanCodogno\DoctrineSnowflakeIdBundle\SnowflakeIdGenerator; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Product { #[ORM\Id] #[ORM\Column(type: 'bigint')] #[ORM\GeneratedValue(strategy: 'CUSTOM')] #[ORM\CustomIdGenerator(class: SnowflakeIdGenerator::class)] private ?string $id = null; // ... }
β³οΈ (Doctrine ORM) Assigning Snowflake ID to any column
Use the #[SnowflakeColumn] attribute to mark any non-ID field for automatic generation:
use JeanCodogno\DoctrineSnowflakeIdBundle\Attributes\SnowflakeColumn; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Product { #[ORM\Column(type: 'bigint', unique: true)] #[SnowflakeColumn] private ?string $publicId = null; // ... }
(Doctrine ODM) Using Snowflake ID Generator
Use the MongoSnowflakeIdGenerator class with Doctrine ODMβs custom ID generation:
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use JeanCodogno\DoctrineSnowflakeIdBundle\IdGenerator\MongoSnowflakeIdGenerator; #[ODM\Document(collection: 'products')] class Product { #[ODM\Id(strategy: 'CUSTOM', type: 'string', options: ['class' => MongoSnowflakeIdGenerator::class])] private ?string $id; // ...
(Doctrine ODM) Assigning Snowflake ID to any field
use the #[SnowflakeColumn] attribute to marky any field for automatic generation:
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use JeanCodogno\DoctrineSnowflakeIdBundle\Attributes\SnowflakeField; #[ODM\Document(collection: 'products')] class Product { #[SnowflakeField] private ?string $public_id = null; // ...
π§ Configuration
By default, DoctrineSnowflakeIdBundle works without any configuration, using default values for datacenterId, workerId, and startTimestamp.
If you want to customize these values, you can define the following parameters in your Symfony configuration:
#config/services.yaml parameters: snowflake_id.datacenter_id: 2 # Default: 0 snowflake_id.worker_id: 7 # Default: 0 snowflake_id.start_timestamp: 1672531200000 # Optional β e.g., Jan 1, 2023 in milliseconds
π§ͺ Testing
You can test ID assignment with tools like PHPUnit or Pest. Snowflake IDs are generated before persist, ensuring uniqueness without collisions.
π License
This bundle is open-source software licensed under the MIT license