Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Add support for doctrine/orm:^3.0 #1468

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Autocomplete/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

- Add doctrine/orm 3 support.

## 2.14.0

- Fixed behavior of Autocomplete when the underlying `select` or `option`
Expand Down
3 changes: 2 additions & 1 deletion src/Autocomplete/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require": {
"php": ">=8.1",
"symfony/dependency-injection": "^6.3|^7.0",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/http-foundation": "^6.3|^7.0",
"symfony/http-kernel": "^6.3|^7.0",
"symfony/property-access": "^6.3|^7.0",
evertharmeling marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -34,7 +35,7 @@
"require-dev": {
"doctrine/collections": "^1.6.8|^2.0",
"doctrine/doctrine-bundle": "^2.4.3",
"doctrine/orm": "^2.9.4",
"doctrine/orm": "^2.9.4|^3.0",
"fakerphp/faker": "^1.22",
"mtdowling/jmespath.php": "^2.6",
"symfony/form": "^6.3|^7.0",
Expand Down
38 changes: 35 additions & 3 deletions src/Autocomplete/src/Doctrine/EntityMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,53 @@ public function isEmbeddedClassProperty(string $propertyName): bool
}

public function getPropertyMetadata(string $propertyName): array
{
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.');

try {
return $this->getFieldMetadata($propertyName);
} catch (\InvalidArgumentException $e) {
return $this->getAssociationMetadata($propertyName);
}
}

/**
* @internal
*
* @return array<string, mixed>
*/
public function getFieldMetadata(string $propertyName): array
evertharmeling marked this conversation as resolved.
Show resolved Hide resolved
{
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
return $this->metadata->fieldMappings[$propertyName];
// Cast to array, because in doctrine/orm:^3.0; $metadata will be a FieldMapping object
return (array) $this->metadata->fieldMappings[$propertyName];
}

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

/**
* @internal
*
* @return array<string, mixed>
*/
public function getAssociationMetadata(string $propertyName): array
evertharmeling marked this conversation as resolved.
Show resolved Hide resolved
{
if (\array_key_exists($propertyName, $this->metadata->associationMappings)) {
return $this->metadata->associationMappings[$propertyName];
// Cast to array, because in doctrine/orm:^3.0; $metadata will be an AssociationMapping object
return (array) $this->metadata->associationMappings[$propertyName];
}

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

public function getPropertyDataType(string $propertyName): string
{
return $this->getPropertyMetadata($propertyName)['type'];
if (\array_key_exists($propertyName, $this->metadata->fieldMappings)) {
return $this->getFieldMetadata($propertyName)['type'];
}

return $this->getAssociationMetadata($propertyName)['type'];
}

public function getIdValue(object $entity): string
Expand Down
7 changes: 3 additions & 4 deletions src/Autocomplete/src/Doctrine/EntitySearchUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function addSearchClause(QueryBuilder $queryBuilder, string $query, strin
}

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

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

if ($i < $numAssociatedProperties - 2) {
$propertyMetadata = $associatedEntityDto->getPropertyMetadata($associatedPropertyName);
$targetEntity = $propertyMetadata['targetEntity'];
$associatedEntityDto = $this->metadataFactory->create($targetEntity);
$propertyMetadata = $associatedEntityDto->getAssociationMetadata($associatedPropertyName);
evertharmeling marked this conversation as resolved.
Show resolved Hide resolved
$associatedEntityDto = $this->metadataFactory->create($propertyMetadata['targetEntity']);
}
}

Expand Down
45 changes: 43 additions & 2 deletions src/Autocomplete/tests/Integration/Doctrine/EntityMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testGetPropertyDataType(): void
$this->assertEquals(ClassMetadataInfo::MANY_TO_ONE, $metadata->getPropertyDataType('category'));
}

public function testGetPropertyMetadata(): void
public function testGetFieldMetadata(): void
{
$metadata = $this->getMetadata();
$this->assertSame([
Expand All @@ -66,7 +66,48 @@ public function testGetPropertyMetadata(): void
'nullable' => false,
'precision' => null,
'columnName' => 'name',
], $metadata->getPropertyMetadata('name'));
], $metadata->getFieldMetadata('name'));
}

public function testGetAssociationMetadata(): void
{
$metadata = $this->getMetadata();
$this->assertSame([
'fieldName' => 'category',
'joinColumns' => [
[
'name' => 'category_id',
'unique' => false,
'nullable' => false,
'onDelete' => null,
'columnDefinition' => null,
'referencedColumnName' => 'id',
],
],
'cascade' => [],
'inversedBy' => 'products',
'targetEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category',
'fetch' => 2,
'type' => 2,
'mappedBy' => null,
'isOwningSide' => true,
'sourceEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Product',
'isCascadeRemove' => false,
'isCascadePersist' => false,
'isCascadeRefresh' => false,
'isCascadeMerge' => false,
'isCascadeDetach' => false,
'sourceToTargetKeyColumns' => [
'category_id' => 'id',
],
'joinColumnFieldNames' => [
'category_id' => 'category_id',
],
'targetToSourceKeyColumns' => [
'id' => 'category_id',
],
'orphanRemoval' => false,
], $metadata->getAssociationMetadata('category'));
}

public function testIsEmbeddedClassProperty(): void
Expand Down