headsnet / doctrine-tools-bundle
Tools and helpers for using Doctrine in larger Symfony projects
Installs: 3 291
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 1
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
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.