|
2 | 2 |
|
3 | 3 | namespace Illuminate\Database;
|
4 | 4 |
|
| 5 | +use Doctrine\DBAL\Types\Type; |
5 | 6 | use Illuminate\Database\Connectors\ConnectionFactory;
|
6 | 7 | use Illuminate\Support\Arr;
|
7 | 8 | use Illuminate\Support\ConfigurationUrlParser;
|
8 | 9 | use Illuminate\Support\Str;
|
9 | 10 | use InvalidArgumentException;
|
10 | 11 | use PDO;
|
| 12 | +use RuntimeException; |
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | * @mixin \Illuminate\Database\Connection
|
@@ -49,6 +51,13 @@ class DatabaseManager implements ConnectionResolverInterface
|
49 | 51 | */
|
50 | 52 | protected $reconnector;
|
51 | 53 |
|
| 54 | + /** |
| 55 | + * The custom Doctrine column types. |
| 56 | + * |
| 57 | + * @var array |
| 58 | + */ |
| 59 | + protected $doctrineTypes = []; |
| 60 | + |
52 | 61 | /**
|
53 | 62 | * Create a new database manager instance.
|
54 | 63 | *
|
@@ -207,16 +216,46 @@ protected function setPdoForType(Connection $connection, $type = null)
|
207 | 216 | }
|
208 | 217 |
|
209 | 218 | /**
|
210 |
| - * Register custom Doctrine types from the configuration with the connection. |
| 219 | + * Register custom Doctrine types with the connection. |
211 | 220 | *
|
212 | 221 | * @param \Illuminate\Database\Connection $connection
|
213 | 222 | * @return void
|
214 | 223 | */
|
215 | 224 | protected function registerConfiguredDoctrineTypes(Connection $connection): void
|
216 | 225 | {
|
217 | 226 | 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 | + ); |
219 | 252 | }
|
| 253 | + |
| 254 | + if (! Type::hasType($name)) { |
| 255 | + Type::addType($name, $class); |
| 256 | + } |
| 257 | + |
| 258 | + $this->doctrineTypes[$name] = [$type, $class]; |
220 | 259 | }
|
221 | 260 |
|
222 | 261 | /**
|
|
0 commit comments