Skip to content

Instantly share code, notes, and snippets.

@benr77
Created August 13, 2022 15:11
Show Gist options
  • Save benr77/d8ec2dc968d8c94a6450bcc79ccda9b0 to your computer and use it in GitHub Desktop.
Save benr77/d8ec2dc968d8c94a6450bcc79ccda9b0 to your computer and use it in GitHub Desktop.
Auto-registering a Doctrine custom type in Symfony bundle configuration
<?php
declare(strict_types=1);
namespace Acme\DemoBundle;
use Acme\DemoBundle\Doctrine\DBAL\Types\MyCustomType;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeDemoBundle extends Bundle
{
public function boot(): void
{
if (!Type::hasType('my_type')) {
Type::addType('my_type', MyCustomType::class);
}
/** @var ManagerRegistry $registry */
$registry = $this->container->get('doctrine');
foreach ($registry->getConnections() as $connection) {
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('my_type', 'my_type');
}
}
}
@benr77
Copy link
Author

benr77 commented Aug 13, 2022

When creating a third-party bundle for Symfony, instead of asking users of the bundle to add the custom type to their doctrine.yaml configuration, you can include this in your bundle and when the bundle is loaded it will automatically register the customer DBAL type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment