Skip to content

Commit 4bd9f59

Browse files
committed
fix(doctrine): read the column default option with doctrine/orm 3
1 parent c33a566 commit 4bd9f59

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ 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+
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping->options['default'] ?? $fieldMapping->default ?? $propertyMetadata->getDefault());
7979
} else {
8080
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());
8181
}

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

Lines changed: 22 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;
@@ -126,6 +128,26 @@ public function testCreateWithDefaultOption(): void
126128
$this->assertSame($doctrinePropertyMetadata->getDefault(), 'default value');
127129
}
128130

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

0 commit comments

Comments
 (0)