Skip to content

headsnet/doctrine-tools-bundle

Repository files navigation

Doctrine Tools for Symfony

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

Various tools and helpers for using Doctrine ORM in larger Symfony projects.

Installation

composer require headsnet/doctrine-tools

If your Symfony installation does not auto-register bundles, add it manually:

// bundles.php
return [
    ...
    Headsnet\DoctrineToolsBundle\HeadsnetDoctrineToolsBundle::class => ['all' => true],
];

Features

Auto-Register Custom Doctrine Type Mappings

The bundle can auto-register custom Doctrine DBAL types, eliminating the need to specify them all in config/packages/doctrine.yaml:

Define the directories to scan for Doctrine types:

headsnet_doctrine_tools:
  custom_mappings:
    scan_dirs:
      - 'src/Infra/Persistence/DBAL/Types'

Then add the #[DoctrineTypeMapping] attribute to the custom type class:

use Doctrine\DBAL\Types\Type;

#[DoctrineTypeMapping]
final class ReservationIdType extends Type
{
    // defines "reservation_id" type
}

This will register a custom type based on the class name - in this case the custom column type will be called reservation_id.

To customise the type name, specify it in the #[DoctrineTypeMapping] attribute. The following will register a type called my_reservation_id.

#[DoctrineTypeMapping(name: 'my_reservation_id')]
final class ReservationIdType extends Type
{
    // customised name "my_reservation_id" type
}

Auto-Register Carbon datetime type mappings

If the nesbot/carbon package is installed, this package will automatically register the Doctrine types provided by Carbon.

By default, it will overwrite the default Doctrine types for datetime and datetime_immutable with the Carbon equivalents:

datetime_immutable: \Carbon\Doctrine\DateTimeImmutableType
datetime: \Carbon\Doctrine\DateTimeType

If you wish the Carbon types to operate alongside the default DateTime and DateTimeImmutable types, set replace: false in the bundle configuration. This will result in additional types being defined for the Carbon columns.

carbon_immutable: \Carbon\Doctrine\DateTimeImmutableType
carbon: \Carbon\Doctrine\DateTimeType

If you wish to completely disable this behaviour, set enabled: false in the bundle configuration.

headsnet_doctrine_tools:
  carbon_mappings:
    enabled: true
    replace: true

Contributions

Contributions are welcome via Pull Requests. Please include a single change in each PR.

License

Released under the MIT License.