Skip to content

Commit 20f4dad

Browse files
evertharmelingweaverryan
authored andcommitted
[Autocomplete] Add support for doctrine/orm:^3.0
1 parent 30c8dbc commit 20f4dad

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Unreleased
4+
5+
- Add doctrine/orm 3 support.
6+
37
## 2.14.0
48

59
- Fixed behavior of Autocomplete when the underlying `select` or `option`

src/Autocomplete/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"require": {
2727
"php": ">=8.1",
2828
"symfony/dependency-injection": "^6.3|^7.0",
29+
"symfony/deprecation-contracts": "^2.5|^3",
2930
"symfony/http-foundation": "^6.3|^7.0",
3031
"symfony/http-kernel": "^6.3|^7.0",
3132
"symfony/property-access": "^6.3|^7.0",
@@ -34,7 +35,7 @@
3435
"require-dev": {
3536
"doctrine/collections": "^1.6.8|^2.0",
3637
"doctrine/doctrine-bundle": "^2.4.3",
37-
"doctrine/orm": "^2.9.4",
38+
"doctrine/orm": "^2.9.4|^3.0",
3839
"fakerphp/faker": "^1.22",
3940
"mtdowling/jmespath.php": "^2.6",
4041
"symfony/form": "^6.3|^7.0",

src/Autocomplete/src/Doctrine/EntityMetadata.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,53 @@ public function isEmbeddedClassProperty(string $propertyName): bool
4242
}
4343

4444
public function getPropertyMetadata(string $propertyName): array
45+
{
46+
trigger_deprecation('symfony/ux-autocomplete', '2.15.0', 'Calling EntityMetadata::getPropertyMetadata() is deprecated. You should stop using it, as it will be removed in the future.');
47+
48+
try {
49+
return $this->getFieldMetadata($propertyName);
50+
} catch (\InvalidArgumentException $e) {
51+
return $this->getAssociationMetadata($propertyName);
52+
}
53+
}
54+
55+
/**
56+
* @internal
57+
*
58+
* @return array<string, mixed>
59+
*/
60+
public function getFieldMetadata(string $propertyName): array
4561
{
4662
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
47-
return $this->metadata->fieldMappings[$propertyName];
63+
// Cast to array, because in doctrine/orm:^3.0; $metadata will be a FieldMapping object
64+
return (array) $this->metadata->fieldMappings[$propertyName];
4865
}
4966

67+
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
68+
}
69+
70+
/**
71+
* @internal
72+
*
73+
* @return array<string, mixed>
74+
*/
75+
public function getAssociationMetadata(string $propertyName): array
76+
{
5077
if (\array_key_exists($propertyName, $this->metadata->associationMappings)) {
51-
return $this->metadata->associationMappings[$propertyName];
78+
// Cast to array, because in doctrine/orm:^3.0; $metadata will be an AssociationMapping object
79+
return (array) $this->metadata->associationMappings[$propertyName];
5280
}
5381

5482
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
5583
}
5684

5785
public function getPropertyDataType(string $propertyName): string
5886
{
59-
return $this->getPropertyMetadata($propertyName)['type'];
87+
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
88+
return $this->getFieldMetadata($propertyName)['type'];
89+
}
90+
91+
return $this->getAssociationMetadata($propertyName)['type'];
6092
}
6193

6294
public function getIdValue(object $entity): string

src/Autocomplete/src/Doctrine/EntitySearchUtil.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function addSearchClause(QueryBuilder $queryBuilder, string $query, strin
6060
}
6161

6262
$originalPropertyName = $associatedProperties[0];
63-
$originalPropertyMetadata = $entityMetadata->getPropertyMetadata($originalPropertyName);
63+
$originalPropertyMetadata = $entityMetadata->getAssociationMetadata($originalPropertyName);
6464
$associatedEntityDto = $this->metadataFactory->create($originalPropertyMetadata['targetEntity']);
6565

6666
for ($i = 0; $i < $numAssociatedProperties - 1; ++$i) {
@@ -75,9 +75,8 @@ public function addSearchClause(QueryBuilder $queryBuilder, string $query, strin
7575
}
7676

7777
if ($i < $numAssociatedProperties - 2) {
78-
$propertyMetadata = $associatedEntityDto->getPropertyMetadata($associatedPropertyName);
79-
$targetEntity = $propertyMetadata['targetEntity'];
80-
$associatedEntityDto = $this->metadataFactory->create($targetEntity);
78+
$propertyMetadata = $associatedEntityDto->getAssociationMetadata($associatedPropertyName);
79+
$associatedEntityDto = $this->metadataFactory->create($propertyMetadata['targetEntity']);
8180
}
8281
}
8382

src/Autocomplete/tests/Integration/Doctrine/EntityMetadataTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testGetPropertyDataType(): void
5454
$this->assertEquals(ClassMetadataInfo::MANY_TO_ONE, $metadata->getPropertyDataType('category'));
5555
}
5656

57-
public function testGetPropertyMetadata(): void
57+
public function testGetFieldMetadata(): void
5858
{
5959
$metadata = $this->getMetadata();
6060
$this->assertSame([
@@ -66,7 +66,48 @@ public function testGetPropertyMetadata(): void
6666
'nullable' => false,
6767
'precision' => null,
6868
'columnName' => 'name',
69-
], $metadata->getPropertyMetadata('name'));
69+
], $metadata->getFieldMetadata('name'));
70+
}
71+
72+
public function testGetAssociationMetadata(): void
73+
{
74+
$metadata = $this->getMetadata();
75+
$this->assertSame([
76+
'fieldName' => 'category',
77+
'joinColumns' => [
78+
[
79+
'name' => 'category_id',
80+
'unique' => false,
81+
'nullable' => false,
82+
'onDelete' => null,
83+
'columnDefinition' => null,
84+
'referencedColumnName' => 'id',
85+
],
86+
],
87+
'cascade' => [],
88+
'inversedBy' => 'products',
89+
'targetEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category',
90+
'fetch' => 2,
91+
'type' => 2,
92+
'mappedBy' => null,
93+
'isOwningSide' => true,
94+
'sourceEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Product',
95+
'isCascadeRemove' => false,
96+
'isCascadePersist' => false,
97+
'isCascadeRefresh' => false,
98+
'isCascadeMerge' => false,
99+
'isCascadeDetach' => false,
100+
'sourceToTargetKeyColumns' => [
101+
'category_id' => 'id',
102+
],
103+
'joinColumnFieldNames' => [
104+
'category_id' => 'category_id',
105+
],
106+
'targetToSourceKeyColumns' => [
107+
'id' => 'category_id',
108+
],
109+
'orphanRemoval' => false,
110+
], $metadata->getAssociationMetadata('category'));
70111
}
71112

72113
public function testIsEmbeddedClassProperty(): void

0 commit comments

Comments
 (0)