Skip to content

Commit 0aa5088

Browse files
authored
fix(doctrine): read the column default option with doctrine/orm 3 (#8705)
1 parent 82188af commit 0aa5088

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public function create(string $resourceClass, string $property, array $options =
7575
if ($doctrineClassMetadata instanceof ClassMetadata && \in_array($property, $doctrineClassMetadata->getFieldNames(), true)) {
7676
$fieldMapping = $doctrineClassMetadata->getFieldMapping($property);
7777
if (class_exists(FieldMapping::class)) {
78-
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping->default ?? $propertyMetadata->getDefault());
78+
// TODO: remove the $fieldMapping->default fallback with doctrine/orm 4.0 (deprecated since 3.6, removed in 4.0)
79+
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping->options['default'] ?? $fieldMapping->default ?? $propertyMetadata->getDefault());
7980
} else {
8081
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());
8182
}

‎src/Doctrine/Orm/Tests/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
use ApiPlatform\Metadata\ApiProperty;
2020
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2121
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
22+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
2223
use Doctrine\ORM\Mapping\FieldMapping;
2324
use Doctrine\Persistence\ManagerRegistry;
2425
use Doctrine\Persistence\Mapping\ClassMetadata;
26+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
2527
use Doctrine\Persistence\ObjectManager;
2628
use PHPUnit\Framework\TestCase;
2729
use Prophecy\PhpUnit\ProphecyTrait;
@@ -106,6 +108,7 @@ public function testCreateWithDefaultOption(): void
106108
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn(['dummyDefaultOption']);
107109
if (class_exists(FieldMapping::class)) {
108110
$fieldMapping = new FieldMapping('string', 'dummyDefaultOption', 'dummyDefaultOption');
111+
// TODO: FieldMapping::$default is removed in doctrine/orm 4.0, use $fieldMapping->options = ['default' => 'default value'] instead
109112
$fieldMapping->default = 'default value';
110113
$classMetadata->fieldMappings = [$fieldMapping];
111114
$classMetadata->getFieldMapping('dummyDefaultOption')->willReturn($fieldMapping);
@@ -126,6 +129,26 @@ public function testCreateWithDefaultOption(): void
126129
$this->assertSame($doctrinePropertyMetadata->getDefault(), 'default value');
127130
}
128131

132+
public function testCreateWithDefaultOptionFromColumnAttribute(): void
133+
{
134+
$classMetadata = new ORMClassMetadata(DummyPropertyWithDefaultValue::class);
135+
$classMetadata->initializeReflection(new RuntimeReflectionService());
136+
(new AttributeDriver([]))->loadMetadataForClass(DummyPropertyWithDefaultValue::class, $classMetadata);
137+
138+
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);
139+
$propertyMetadataFactory->method('create')->willReturn(new ApiProperty());
140+
141+
$objectManager = $this->createStub(ObjectManager::class);
142+
$objectManager->method('getClassMetadata')->willReturn($classMetadata);
143+
144+
$managerRegistry = $this->createStub(ManagerRegistry::class);
145+
$managerRegistry->method('getManagerForClass')->willReturn($objectManager);
146+
147+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry, $propertyMetadataFactory);
148+
149+
$this->assertSame('default value', $doctrineOrmPropertyMetadataFactory->create(DummyPropertyWithDefaultValue::class, 'dummyDefaultOption')->getDefault());
150+
}
151+
129152
public function testCreateClassMetadataInfo(): void
130153
{
131154
$propertyMetadata = new ApiProperty();

0 commit comments

Comments
 (0)