tourze / doctrine-precision-bundle
Doctrine精度增强
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^2.20 || ^3.0
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- yiisoft/strings: ^2.1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-25 18:35:20 UTC
README
A Symfony bundle that enhances Doctrine ORM by providing precision control for decimal fields.
Features
- Automatically applies consistent precision to decimal fields across your application
- Uses environment variables to configure precision values
- Simple attribute-based implementation
- Seamless integration with Doctrine ORM
Installation
composer require tourze/doctrine-precision-bundle
Quick Start
1. Register the bundle
The bundle should be automatically registered if you're using Symfony Flex. If not, add it to your config/bundles.php
:
<?php return [ // ... Tourze\DoctrinePrecisionBundle\DoctrinePrecisionBundle::class => ['all' => true], ];
2. Configure precision value
Set the default precision value in your .env
file:
DEFAULT_PRICE_PRECISION=2
3. Use the attribute in your entities
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrinePrecisionBundle\Attribute\PrecisionColumn; #[ORM\Entity] class Product { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(type: 'decimal', precision: 10)] #[PrecisionColumn] private ?string $price = null; // getters and setters }
The PrecisionColumn
attribute will automatically set the scale (decimal places) according to the DEFAULT_PRICE_PRECISION
environment variable.
How It Works
The bundle registers a Doctrine event listener that intercepts the loadClassMetadata
event. When it finds properties marked with the PrecisionColumn
attribute, it sets the scale value for decimal fields based on the environment configuration.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This bundle is released under the MIT License. See the bundled LICENSE file for details.