Created
August 13, 2022 15:11
-
-
Save benr77/d8ec2dc968d8c94a6450bcc79ccda9b0 to your computer and use it in GitHub Desktop.
Auto-registering a Doctrine custom type in Symfony bundle configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.