tourze / doctrine-random-bundle
Doctrine随机增强
Requires
- php: ^8.1
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^2.20 || ^3.0
- doctrine/persistence: ^3.1 || ^4
- psr/log: ^3|^2|^1
- symfony/cache-contracts: ^3
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/lock: ^6.4
- symfony/property-access: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/bundle-dependency: 0.0.*
- tourze/doctrine-entity-checker-bundle: ~0.0.8
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-25 14:29:02 UTC
README
A Symfony bundle that provides automatic random string generation for Doctrine entity properties using PHP attributes.
Features
- Generate random string values for entity properties
- Configurable prefix and string length
- Automatic value generation on entity creation (Doctrine prePersist event)
- Skips generation if property already has a value
- Simple integration with Symfony auto-configuration
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine Bundle 2.13 or higher
- Doctrine ORM 2.20/3.0 or higher
Installation
composer require tourze/doctrine-random-bundle
This bundle is auto-registered by Symfony Flex. No extra configuration is required.
Quick Start
Add the RandomStringColumn
attribute to your entity property:
use Tourze\DoctrineRandomBundle\Attribute\RandomStringColumn; class YourEntity { #[RandomStringColumn(prefix: 'user_', length: 20)] private string $randomId; public function getRandomId(): string { return $this->randomId; } public function setRandomId(string $randomId): self { $this->randomId = $randomId; return $this; } }
When you persist a new entity, the randomId
property will be automatically filled if it is empty:
$entity = new YourEntity(); $entityManager->persist($entity); $entityManager->flush(); // $entity->getRandomId() will return something like 'user_a1b2c3d4e5f6g7h8i9'
Configuration
The RandomStringColumn
attribute accepts the following parameters:
prefix
: String prefix for the random value (default: '')length
: Length of the random string (default: 16)
Advanced Details
- The bundle uses a Doctrine event listener (
RandomStringListener
) to automatically generate random strings for properties marked with theRandomStringColumn
attribute during theprePersist
event. - If the property already has a value, it will not be overwritten.
- The random string is composed of numbers and upper/lowercase letters.
Contribution Guide
Contributions are welcome! To contribute:
- Open an issue for bug reports or feature requests.
- Submit a pull request with clear description and relevant tests.
- Follow PSR coding standards.
- Run tests with PHPUnit before submitting.
License
This bundle is released under the MIT License. See the LICENSE file for details.
Changelog
- v0.1.0: Initial release with random string attribute and event listener support.
Author
Maintained by tourze.
Configuration
The RandomStringColumn
attribute accepts the following parameters:
prefix
: String prefix for the random value (default: '')length
: Length of the random string (default: 16)
Example
// Create a new entity $entity = new YourEntity(); // The randomId property will be automatically filled with a random string // when the entity is persisted $entityManager->persist($entity); $entityManager->flush(); // Now $entity->getRandomId() will return something like 'user_a1b2c3d4e5f6g7h8i9'
License
This bundle is available under the MIT license. See the LICENSE file for more information.