headsnet / doctrine-tools-bundle
为使用Doctrine在大型Symfony项目中提供工具和助手
v0.2.1
2024-05-25 04:43 UTC
Requires
- php: >=8.1
- doctrine/dbal: ^3.0 || ^4.0
- doctrine/doctrine-bundle: ^2.0
- league/construct-finder: ^1.3
- symfony/config: ^6.0 || ^7.0
- symfony/dependency-injection: ^6.0 || ^7.0
- symfony/http-kernel: ^6.0 || ^7.0
Requires (Dev)
- nesbot/carbon: ^3.3
- nyholm/symfony-bundle-test: ^3.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.0
- symplify/easy-coding-standard: ^12.2
Suggests
- nesbot/carbon: If present, this package will automatically register the Carbon Doctrine types.
README
用于在大型Symfony项目中使用Doctrine ORM的各种工具和助手。
安装
composer require headsnet/doctrine-tools
如果您的Symfony安装没有自动注册包,请手动添加
// bundles.php return [ ... Headsnet\DoctrineToolsBundle\HeadsnetDoctrineToolsBundle::class => ['all' => true], ];
功能
自动注册自定义Doctrine类型映射
该包可以自动注册自定义Doctrine DBAL类型,从而无需在 config/packages/doctrine.yaml
中指定它们
定义扫描Doctrine类型的目录
headsnet_doctrine_tools: custom_mappings: scan_dirs: - 'src/Infra/Persistence/DBAL/Types'
然后添加 #[DoctrineTypeMapping]
属性到自定义类型类
use Doctrine\DBAL\Types\Type; #[DoctrineTypeMapping] final class ReservationIdType extends Type { // defines "reservation_id" type }
这将基于类名注册自定义类型 - 在这种情况下,自定义列类型将称为 reservation_id
。
要自定义类型名称,在 #[DoctrineTypeMapping]
属性中指定它。以下将注册一个名为 my_reservation_id
的类型。
#[DoctrineTypeMapping(name: 'my_reservation_id')] final class ReservationIdType extends Type { // customised name "my_reservation_id" type }
自动注册Carbon日期时间类型映射
如果已安装 nesbot/carbon
包,则此包将自动注册Carbon提供的Doctrine类型。
默认情况下,它将使用Carbon等效项覆盖默认的 datetime
和 datetime_immutable
Doctrine类型
datetime_immutable: \Carbon\Doctrine\DateTimeImmutableType datetime: \Carbon\Doctrine\DateTimeType
如果您希望Carbon类型与默认的DateTime和DateTimeImmutable类型一起操作,请在包配置中将 replace: false
设置。这将导致为Carbon列定义额外的类型。
carbon_immutable: \Carbon\Doctrine\DateTimeImmutableType carbon: \Carbon\Doctrine\DateTimeType
如果您希望完全禁用此行为,请在包配置中将 enabled: false
设置。
headsnet_doctrine_tools: carbon_mappings: enabled: true replace: true
贡献
欢迎通过Pull Requests进行贡献。请在每个PR中包含单个更改。
许可
在 MIT许可证 下发布。