headsnet/doctrine-tools-bundle

为使用Doctrine在大型Symfony项目中提供工具和助手

v0.2.1 2024-05-25 04:43 UTC

This package is auto-updated.

Last update: 2024-09-25 07:43:00 UTC


README

Build Status Coverage Latest Stable Version Total Downloads License PHP Version Require

用于在大型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等效项覆盖默认的 datetimedatetime_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许可证 下发布。