diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index e35868eaf3c1..df1a0202657f 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -177,6 +177,13 @@ class Connection implements ConnectionInterface */ protected $doctrineConnection; + /** + * Type mappings that should be registered with new Doctrine connections. + * + * @var array + */ + protected $doctrineTypeMappings = []; + /** * The connection resolvers. * @@ -1007,6 +1014,12 @@ public function getDoctrineConnection() 'driver' => method_exists($driver, 'getName') ? $driver->getName() : null, 'serverVersion' => $this->getConfig('server_version'), ]), $driver); + + foreach ($this->doctrineTypeMappings as $name => $type) { + $this->doctrineConnection + ->getDatabasePlatform() + ->registerDoctrineTypeMapping($type, $name); + } } return $this->doctrineConnection; @@ -1035,9 +1048,7 @@ public function registerDoctrineType(string $class, string $name, string $type): Type::addType($name, $class); } - $this->getDoctrineSchemaManager() - ->getDatabasePlatform() - ->registerDoctrineTypeMapping($type, $name); + $this->doctrineTypeMappings[$name] = $type; } /** diff --git a/src/Illuminate/Database/DatabaseManager.php b/src/Illuminate/Database/DatabaseManager.php index f53523b5b6f7..cb823bfa3de3 100755 --- a/src/Illuminate/Database/DatabaseManager.php +++ b/src/Illuminate/Database/DatabaseManager.php @@ -2,12 +2,14 @@ namespace Illuminate\Database; +use Doctrine\DBAL\Types\Type; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Support\Arr; use Illuminate\Support\ConfigurationUrlParser; use Illuminate\Support\Str; use InvalidArgumentException; use PDO; +use RuntimeException; /** * @mixin \Illuminate\Database\Connection @@ -49,6 +51,13 @@ class DatabaseManager implements ConnectionResolverInterface */ protected $reconnector; + /** + * The custom Doctrine column types. + * + * @var array + */ + protected $doctrineTypes = []; + /** * Create a new database manager instance. * @@ -207,7 +216,7 @@ protected function setPdoForType(Connection $connection, $type = null) } /** - * Register custom Doctrine types from the configuration with the connection. + * Register custom Doctrine types with the connection. * * @param \Illuminate\Database\Connection $connection * @return void @@ -215,8 +224,38 @@ protected function setPdoForType(Connection $connection, $type = null) protected function registerConfiguredDoctrineTypes(Connection $connection): void { foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) { - $connection->registerDoctrineType($class, $name, $name); + $this->registerDoctrineType($class, $name, $name); + } + + foreach ($this->doctrineTypes as $name => [$type, $class]) { + $connection->registerDoctrineType($class, $name, $type); + } + } + + /** + * Register a custom Doctrine type. + * + * @param string $class + * @param string $name + * @param string $type + * @return void + * + * @throws \Doctrine\DBAL\DBALException + * @throws \RuntimeException + */ + public function registerDoctrineType(string $class, string $name, string $type): void + { + if (! class_exists('Doctrine\DBAL\Connection')) { + throw new RuntimeException( + 'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).' + ); } + + if (! Type::hasType($name)) { + Type::addType($name, $class); + } + + $this->doctrineTypes[$name] = [$type, $class]; } /**