Skip to content

Commit

Permalink
[8.x] Fix Doctrine type mappings creating too many connections (#40303)
Browse files Browse the repository at this point in the history
* Only register type mappings after Doctrine connection is created

* Fix type mapping

* Fix

* Fix Builder

* Remove import

* Pass type through to connection

* Update DatabaseManager.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
bakerkretzmar and taylorotwell authored Jan 8, 2022
1 parent 0fb801a commit b0a2d85
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
43 changes: 41 additions & 2 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -207,16 +216,46 @@ 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
*/
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];
}

/**
Expand Down

0 comments on commit b0a2d85

Please sign in to comment.