Skip to content

Commit f756989

Browse files
committed
Fix BIGINT validation
1 parent 8709fb3 commit f756989

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

src/Tools/SchemaValidator.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ class SchemaValidator
6464
* It maps built-in Doctrine types to PHP types
6565
*/
6666
private const BUILTIN_TYPES_MAP = [
67-
AsciiStringType::class => 'string',
68-
BigIntType::class => 'string',
69-
BooleanType::class => 'bool',
70-
DecimalType::class => 'string',
71-
FloatType::class => 'float',
72-
GuidType::class => 'string',
73-
IntegerType::class => 'int',
74-
JsonType::class => 'array',
75-
SimpleArrayType::class => 'array',
76-
SmallIntType::class => 'int',
77-
StringType::class => 'string',
78-
TextType::class => 'string',
67+
AsciiStringType::class => ['string'],
68+
BigIntType::class => ['int', 'string'],
69+
BooleanType::class => ['bool'],
70+
DecimalType::class => ['string'],
71+
FloatType::class => ['float'],
72+
GuidType::class => ['string'],
73+
IntegerType::class => ['int'],
74+
JsonType::class => ['array'],
75+
SimpleArrayType::class => ['array'],
76+
SmallIntType::class => ['int'],
77+
StringType::class => ['string'],
78+
TextType::class => ['string'],
7979
];
8080

8181
public function __construct(EntityManagerInterface $em, bool $validatePropertyTypes = true)
@@ -390,21 +390,21 @@ function (array $fieldMapping) use ($class): ?string {
390390
$propertyType = $propertyType->getName();
391391

392392
// If the property type is the same as the metadata field type, we are ok
393-
if ($propertyType === $metadataFieldType) {
393+
if (in_array($propertyType, $metadataFieldType, true)) {
394394
return null;
395395
}
396396

397397
if (is_a($propertyType, BackedEnum::class, true)) {
398398
$backingType = (string) (new ReflectionEnum($propertyType))->getBackingType();
399399

400-
if ($metadataFieldType !== $backingType) {
400+
if (! in_array($backingType, $metadataFieldType, true)) {
401401
return sprintf(
402402
"The field '%s#%s' has the property type '%s' with a backing type of '%s' that differs from the metadata field type '%s'.",
403403
$class->name,
404404
$fieldName,
405405
$propertyType,
406406
$backingType,
407-
$metadataFieldType
407+
implode('|', $metadataFieldType)
408408
);
409409
}
410410

@@ -429,7 +429,7 @@ function (array $fieldMapping) use ($class): ?string {
429429
) {
430430
$backingType = (string) (new ReflectionEnum($fieldMapping['enumType']))->getBackingType();
431431

432-
if ($metadataFieldType === $backingType) {
432+
if (in_array($backingType, $metadataFieldType, true)) {
433433
return null;
434434
}
435435

@@ -439,7 +439,7 @@ function (array $fieldMapping) use ($class): ?string {
439439
$fieldName,
440440
$fieldMapping['enumType'],
441441
$backingType,
442-
$metadataFieldType
442+
implode('|', $metadataFieldType)
443443
);
444444
}
445445

@@ -455,7 +455,7 @@ function (array $fieldMapping) use ($class): ?string {
455455
$class->name,
456456
$fieldName,
457457
$propertyType,
458-
$metadataFieldType,
458+
implode('|', $metadataFieldType),
459459
$fieldMapping['type']
460460
);
461461
},
@@ -468,8 +468,10 @@ function (array $fieldMapping) use ($class): ?string {
468468
/**
469469
* The exact DBAL type must be used (no subclasses), since consumers of doctrine/orm may have their own
470470
* customization around field types.
471+
*
472+
* @return list<string>|null
471473
*/
472-
private function findBuiltInType(Type $type): ?string
474+
private function findBuiltInType(Type $type): ?array
473475
{
474476
$typeName = get_class($type);
475477

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\BigIntegers;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
/** @ORM\Entity */
10+
class BigIntegers
11+
{
12+
/**
13+
* @ORM\Column(type="integer")
14+
* @ORM\Id
15+
* @ORM\GeneratedValue
16+
*/
17+
public ?int $id = null;
18+
19+
/** @ORM\Column(type="bigint") */
20+
public int $one = 1;
21+
22+
/** @ORM\Column(type="bigint") */
23+
public string $two = '2';
24+
25+
/** @ORM\Column(type="bigint") */
26+
public float $three = 3.0;
27+
}

tests/Tests/ORM/Tools/SchemaValidatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Doctrine\ORM\Mapping\OrderBy;
2525
use Doctrine\ORM\Mapping\Table;
2626
use Doctrine\ORM\Tools\SchemaValidator;
27+
use Doctrine\Tests\Models\BigIntegers\BigIntegers;
2728
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
2829
use Doctrine\Tests\OrmTestCase;
2930

@@ -240,6 +241,19 @@ public function testInvalidAssociationTowardsMappedSuperclass(): void
240241
$ce
241242
);
242243
}
244+
245+
/**
246+
* @requires PHP 7.4
247+
*/
248+
public function testBigIntProperty(): void
249+
{
250+
$class = $this->em->getClassMetadata(BigIntegers::class);
251+
252+
self::assertSame(
253+
['The field \'Doctrine\Tests\Models\BigIntegers\BigIntegers#three\' has the property type \'float\' that differs from the metadata field type \'int|string\' returned by the \'bigint\' DBAL type.'],
254+
$this->validator->validateClass($class)
255+
);
256+
}
243257
}
244258

245259
/** @MappedSuperclass */

0 commit comments

Comments
 (0)