Skip to content

Commit b0a2d85

Browse files
[8.x] Fix Doctrine type mappings creating too many connections (#40303)
* 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]>
1 parent 0fb801a commit b0a2d85

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ class Connection implements ConnectionInterface
177177
*/
178178
protected $doctrineConnection;
179179

180+
/**
181+
* Type mappings that should be registered with new Doctrine connections.
182+
*
183+
* @var array
184+
*/
185+
protected $doctrineTypeMappings = [];
186+
180187
/**
181188
* The connection resolvers.
182189
*
@@ -1007,6 +1014,12 @@ public function getDoctrineConnection()
10071014
'driver' => method_exists($driver, 'getName') ? $driver->getName() : null,
10081015
'serverVersion' => $this->getConfig('server_version'),
10091016
]), $driver);
1017+
1018+
foreach ($this->doctrineTypeMappings as $name => $type) {
1019+
$this->doctrineConnection
1020+
->getDatabasePlatform()
1021+
->registerDoctrineTypeMapping($type, $name);
1022+
}
10101023
}
10111024

10121025
return $this->doctrineConnection;
@@ -1035,9 +1048,7 @@ public function registerDoctrineType(string $class, string $name, string $type):
10351048
Type::addType($name, $class);
10361049
}
10371050

1038-
$this->getDoctrineSchemaManager()
1039-
->getDatabasePlatform()
1040-
->registerDoctrineTypeMapping($type, $name);
1051+
$this->doctrineTypeMappings[$name] = $type;
10411052
}
10421053

10431054
/**

src/Illuminate/Database/DatabaseManager.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Illuminate\Database;
44

5+
use Doctrine\DBAL\Types\Type;
56
use Illuminate\Database\Connectors\ConnectionFactory;
67
use Illuminate\Support\Arr;
78
use Illuminate\Support\ConfigurationUrlParser;
89
use Illuminate\Support\Str;
910
use InvalidArgumentException;
1011
use PDO;
12+
use RuntimeException;
1113

1214
/**
1315
* @mixin \Illuminate\Database\Connection
@@ -49,6 +51,13 @@ class DatabaseManager implements ConnectionResolverInterface
4951
*/
5052
protected $reconnector;
5153

54+
/**
55+
* The custom Doctrine column types.
56+
*
57+
* @var array
58+
*/
59+
protected $doctrineTypes = [];
60+
5261
/**
5362
* Create a new database manager instance.
5463
*
@@ -207,16 +216,46 @@ protected function setPdoForType(Connection $connection, $type = null)
207216
}
208217

209218
/**
210-
* Register custom Doctrine types from the configuration with the connection.
219+
* Register custom Doctrine types with the connection.
211220
*
212221
* @param \Illuminate\Database\Connection $connection
213222
* @return void
214223
*/
215224
protected function registerConfiguredDoctrineTypes(Connection $connection): void
216225
{
217226
foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) {
218-
$connection->registerDoctrineType($class, $name, $name);
227+
$this->registerDoctrineType($class, $name, $name);
228+
}
229+
230+
foreach ($this->doctrineTypes as $name => [$type, $class]) {
231+
$connection->registerDoctrineType($class, $name, $type);
232+
}
233+
}
234+
235+
/**
236+
* Register a custom Doctrine type.
237+
*
238+
* @param string $class
239+
* @param string $name
240+
* @param string $type
241+
* @return void
242+
*
243+
* @throws \Doctrine\DBAL\DBALException
244+
* @throws \RuntimeException
245+
*/
246+
public function registerDoctrineType(string $class, string $name, string $type): void
247+
{
248+
if (! class_exists('Doctrine\DBAL\Connection')) {
249+
throw new RuntimeException(
250+
'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).'
251+
);
219252
}
253+
254+
if (! Type::hasType($name)) {
255+
Type::addType($name, $class);
256+
}
257+
258+
$this->doctrineTypes[$name] = [$type, $class];
220259
}
221260

222261
/**

0 commit comments

Comments
 (0)