Skip to content

Commit cbb6c89

Browse files
authored
Merge pull request #11399 from ThomasLandauer/issue-11377
SchemaValidator: Changing mapping of BIGINT to string|int
2 parents 9c56071 + 753bc16 commit cbb6c89

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

src/Tools/SchemaValidator.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use function in_array;
4040
use function interface_exists;
4141
use function is_a;
42+
use function method_exists;
4243
use function sprintf;
4344

4445
/**
@@ -49,9 +50,9 @@
4950
class SchemaValidator
5051
{
5152
/**
52-
* It maps built-in Doctrine types to PHP types
53+
* Map built-in Doctrine DBAL 3 types to PHP types
5354
*/
54-
private const BUILTIN_TYPES_MAP = [
55+
private const BUILTIN_TYPES_MAP_DBAL3 = [
5556
AsciiStringType::class => 'string',
5657
BigIntType::class => 'string',
5758
BooleanType::class => 'bool',
@@ -66,6 +67,24 @@ class SchemaValidator
6667
TextType::class => 'string',
6768
];
6869

70+
/**
71+
* Map built-in Doctrine DBAL 4+ types to PHP types
72+
*/
73+
private const BUILTIN_TYPES_MAP = [
74+
AsciiStringType::class => 'string',
75+
BigIntType::class => 'string|int',
76+
BooleanType::class => 'bool',
77+
DecimalType::class => 'string',
78+
FloatType::class => 'float',
79+
GuidType::class => 'string',
80+
IntegerType::class => 'int',
81+
JsonType::class => 'array',
82+
SimpleArrayType::class => 'array',
83+
SmallIntType::class => 'int',
84+
StringType::class => 'string',
85+
TextType::class => 'string',
86+
];
87+
6988
public function __construct(
7089
private readonly EntityManagerInterface $em,
7190
private readonly bool $validatePropertyTypes = true,
@@ -436,6 +455,10 @@ private function findBuiltInType(Type $type): string|null
436455
{
437456
$typeName = $type::class;
438457

439-
return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
458+
if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
459+
return self::BUILTIN_TYPES_MAP_DBAL3[$typeName] ?? null;
460+
} else { // DBAL 4+
461+
return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
462+
}
440463
}
441464
}

tests/Tests/ORM/Tools/SchemaValidatorTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Common\Collections\Collection;
9+
use Doctrine\DBAL\Types\BigIntType;
10+
use Doctrine\DBAL\Types\Types;
911
use Doctrine\ORM\EntityManagerInterface;
1012
use Doctrine\ORM\Mapping\Column;
1113
use Doctrine\ORM\Mapping\DiscriminatorMap;
@@ -30,6 +32,8 @@
3032
use PHPUnit\Framework\Attributes\DataProvider;
3133
use PHPUnit\Framework\Attributes\Group;
3234

35+
use function method_exists;
36+
3337
class SchemaValidatorTest extends OrmTestCase
3438
{
3539
private EntityManagerInterface|null $em = null;
@@ -228,6 +232,47 @@ public function testInvalidAssociationTowardsMappedSuperclass(): void
228232
$ce,
229233
);
230234
}
235+
236+
public function testBigintMappedToStringInt(): void
237+
{
238+
$class = $this->em->getClassMetadata(BigintMappedToStringInt::class);
239+
$ce = $this->validator->validateClass($class);
240+
241+
$this->assertEquals([], $ce); // Same for DBAL 3 and 4+
242+
}
243+
244+
public function testBigintMappedToInt(): void
245+
{
246+
$class = $this->em->getClassMetadata(BigintMappedToInt::class);
247+
$ce = $this->validator->validateClass($class);
248+
249+
if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
250+
$this->assertEquals(
251+
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToInt#bigint' has the property type 'int' that differs from the metadata field type 'string' returned by the 'bigint' DBAL type."],
252+
$ce,
253+
);
254+
} else { // DBAL 4+
255+
$this->assertEquals(
256+
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToInt#bigint' has the property type 'int' that differs from the metadata field type 'string|int' returned by the 'bigint' DBAL type."],
257+
$ce,
258+
);
259+
}
260+
}
261+
262+
public function testBigintMappedToString(): void
263+
{
264+
$class = $this->em->getClassMetadata(BigintMappedToString::class);
265+
$ce = $this->validator->validateClass($class);
266+
267+
if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
268+
$this->assertEquals([], $ce);
269+
} else { // DBAL 4+
270+
$this->assertEquals(
271+
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToString#bigint' has the property type 'string' that differs from the metadata field type 'string|int' returned by the 'bigint' DBAL type."],
272+
$ce,
273+
);
274+
}
275+
}
231276
}
232277

233278
#[MappedSuperclass]
@@ -547,3 +592,39 @@ class InvalidMappedSuperClass
547592
#[ManyToMany(targetEntity: 'InvalidMappedSuperClass', mappedBy: 'invalid')]
548593
private $selfWhatever;
549594
}
595+
596+
#[Entity]
597+
class BigintMappedToStringInt
598+
{
599+
#[Id]
600+
#[Column]
601+
#[GeneratedValue]
602+
private int $id;
603+
604+
#[Column(type: Types::BIGINT)]
605+
private string|int $bigint;
606+
}
607+
608+
#[Entity]
609+
class BigintMappedToInt
610+
{
611+
#[Id]
612+
#[Column]
613+
#[GeneratedValue]
614+
private int $id;
615+
616+
#[Column(type: Types::BIGINT)]
617+
private int $bigint;
618+
}
619+
620+
#[Entity]
621+
class BigintMappedToString
622+
{
623+
#[Id]
624+
#[Column]
625+
#[GeneratedValue]
626+
private int $id;
627+
628+
#[Column(type: Types::BIGINT)]
629+
private string $bigint;
630+
}

0 commit comments

Comments
 (0)