tourze / doctrine-snowflake-bundle
为 Doctrine 实体提供雪花ID自动生成功能
Installs: 23 053
Dependents: 58
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-snowflake-bundle
Requires
- ext-reflection: *
- doctrine/common: ^3.5
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- monolog/monolog: ^3.1
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/event-dispatcher: ^7.3
- symfony/expression-language: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/lock: ^7.3
- symfony/messenger: ^7.3
- symfony/property-access: ^7.3
- symfony/string: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-entity-checker-bundle: 1.0.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.*
- tourze/symfony-snowflake-bundle: 1.0.*
Requires (Dev)
Suggests
- ext-redis: *
README
A Symfony bundle that provides Snowflake ID generation for Doctrine entities with distributed unique ID generation capabilities.
Table of Contents
- Features
- Requirements
- Installation
- Configuration
- Quick Start
- Advanced Usage
- API Reference
- Testing
- Performance Considerations
- Contributing
- License
- Changelog
Features
- Snowflake ID Generation: Based on godruoyi/php-snowflake
- Doctrine Integration: Auto-generate Snowflake IDs for Doctrine entity primary keys
- Attribute Support: Use #[SnowflakeColumn]attribute for custom ID properties
- Trait Support: Use SnowflakeKeyAwaretrait for automatic primary key generation
- Distributed Ready: WorkerId auto-generated by hostname, supporting distributed deployment
- Data Center ID: Automatically generated from entity class name for better ID distribution
- String Format: Returns IDs as strings to avoid JavaScript precision issues
- High Performance: Optimized for high concurrency scenarios
Requirements
- PHP 8.1 or newer
- Symfony 7.3 or newer
- Doctrine Bundle 2.13 or newer
- Doctrine ORM 3.0 or newer
- Doctrine DBAL 4.0 or newer
- tourze/symfony-snowflake-bundle
Installation
composer require tourze/doctrine-snowflake-bundle
Configuration
Bundle Registration
The bundle is automatically registered when using Symfony Flex. For manual installation:
// config/bundles.php return [ // ... other bundles Tourze\DoctrineSnowflakeBundle\DoctrineSnowflakeBundle::class => ['all' => true], ];
Quick Start
Method 1: Using SnowflakeKeyAware Trait (Recommended)
For primary key generation:
use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineSnowflakeBundle\Traits\SnowflakeKeyAware; #[ORM\Entity] class YourEntity { use SnowflakeKeyAware; #[ORM\Column(type: 'string', length: 255)] private string $name; // getter/setter methods... }
Method 2: Using SnowflakeColumn Attribute
For custom ID properties (not primary keys):
use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineSnowflakeBundle\Attribute\SnowflakeColumn; #[ORM\Entity] class YourEntity { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private int $id; #[SnowflakeColumn(prefix: 'ORDER_', length: 32)] #[ORM\Column(type: 'string', length: 32)] private string $orderId; // getter/setter methods... }
Method 3: Manual ID Generator Configuration
use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineSnowflakeBundle\Service\SnowflakeIdGenerator; #[ORM\Entity] class YourEntity { #[ORM\Id] #[ORM\GeneratedValue(strategy: 'CUSTOM')] #[ORM\CustomIdGenerator(class: SnowflakeIdGenerator::class)] #[ORM\Column(type: 'bigint', nullable: false)] private string $id; // getter/setter methods... }
Advanced Usage
Custom Configuration
You can customize the Snowflake ID generation behavior:
# config/packages/doctrine_snowflake.yaml doctrine_snowflake: worker_id: 1 # Optional: Override auto-generated worker ID data_center_id: 1 # Optional: Override auto-generated data center ID
Multiple Snowflake Properties
You can use multiple Snowflake properties in a single entity:
#[ORM\Entity] class Order { use SnowflakeKeyAware; // Primary key #[SnowflakeColumn(prefix: 'ORDER_', length: 32)] #[ORM\Column(type: 'string', length: 32)] private string $orderNumber; #[SnowflakeColumn(prefix: 'TXN_', length: 24)] #[ORM\Column(type: 'string', length: 24)] private string $transactionId; }
Performance Tuning
For high-throughput applications:
- Use appropriate database indexes on Snowflake ID columns
- Consider using string column types to avoid integer overflow
- Monitor ID generation performance in distributed environments
API Reference
SnowflakeColumn Attribute
#[SnowflakeColumn(prefix: 'ORDER_', length: 32)]
- prefix: ID prefix (default: empty)
- length: Maximum ID length (default: 0, no limit)
SnowflakeKeyAware Trait
Provides the following methods:
- getId(): ?string- Get the entity ID
- setId(?string $id): void- Set the entity ID
SnowflakeIdGenerator Service
- Automatically generates unique Snowflake IDs
- Uses hostname for worker ID generation
- Uses entity class name for data center ID generation
- Preserves existing IDs when set manually
Testing
Run the test suite:
./vendor/bin/phpunit packages/doctrine-snowflake-bundle/tests
Performance Considerations
- Snowflake IDs are returned as strings to avoid JavaScript precision issues
- The generator uses CRC32 hash of class names for data center ID distribution
- Worker ID is generated from hostname for distributed deployments
- High concurrency scenarios are handled efficiently
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
The MIT License (MIT). Please see License File for more information.
Changelog
See Releases for version history.