Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix Doctrine type mappings creating too many connections #40303

Merged
merged 7 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've kept this whole registerDoctrineType method for backwards compatibility but it can be removed in Laravel 9 because the same method on the DatabaseManager class replaces it. I'll PR that today too.

}

/**
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