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.
✅ 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
Install via Composer:
composer require jeancodogno/doctrine-snowflake-id-bundleThe bundle uses autoconfiguration, no need to manually register it in
bundles.php.
PHP does not have a native type that supports big integers, so the variable must be defined as a
string.
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;
// ...
}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;
// ...
}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;
// ...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;
// ...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 millisecondsYou can test ID assignment with tools like PHPUnit or Pest. Snowflake IDs are generated before persist, ensuring uniqueness without collisions.
This bundle is open-source software licensed under the MIT license