Skip to content

Commit b6871d0

Browse files
authored
Bugfix/custom types cause exception (#1228)
* bugfix/custom-types-cause-exception - Added use statements for DBAL Exception and Type - Added @throws tag to getPropertiesFromTable method - Added Type::addType() to properly register types - Added Try-Catch around Type::addType() to give users a proper error message, then throw the exception up. * bugfix/custom-types-cause-exception - Updated CHANGELOG.MD * bugfix/custom-types-cause-exception - Fixed `Type enum_string_boolean already exists.` error from being triggered when multiple models are loaded
1 parent 3b75048 commit b6871d0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ All notable changes to this project will be documented in this file.
1313
### Added
1414
- Add support of variadic parameters in `ide-helper:models` [\#1234 / shaffe-fr](https://github.com/barryvdh/laravel-ide-helper/pull/1234)
1515

16+
2021-06-18, 2.10.1
17+
------------------
18+
### Added
19+
- Added Type registration according to [Custom Mapping Types documentation](https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types)
20+
21+
### Fixed
22+
- Fixing issue where configured custom_db_types could cause a DBAL exception to be thrown while running `ide-helper:models`
23+
1624
2021-04-09, 2.10.0
1725
------------------
1826
### Added

src/Console/ModelsCommand.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
1818
use Barryvdh\Reflection\DocBlock\Tag;
1919
use Composer\Autoload\ClassMapGenerator;
20+
use Doctrine\DBAL\Exception as DBALException;
21+
use Doctrine\DBAL\Types\Type;
2022
use Illuminate\Console\Command;
2123
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2224
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -415,6 +417,8 @@ protected function getTypeOverride($type)
415417
* Load the properties from the database table.
416418
*
417419
* @param \Illuminate\Database\Eloquent\Model $model
420+
*
421+
* @throws DBALException If custom field failed to register
418422
*/
419423
public function getPropertiesFromTable($model)
420424
{
@@ -426,6 +430,14 @@ public function getPropertiesFromTable($model)
426430
$platformName = $databasePlatform->getName();
427431
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
428432
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
433+
try {
434+
if(!Type::hasType($yourTypeName)) {
435+
Type::addType($yourTypeName, get_class(Type::getType($doctrineTypeName)));
436+
}
437+
} catch (DBALException $exception) {
438+
$this->error("Failed registering custom db type \"$yourTypeName\" as \"$doctrineTypeName\"");
439+
throw $exception;
440+
}
429441
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
430442
}
431443

0 commit comments

Comments
 (0)