Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Factory/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Factory;

use Composer\InstalledVersions;
use Doctrine\DBAL\Types\Types;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
Expand Down Expand Up @@ -105,6 +106,12 @@ private function guessFilterClass(EntityDto $entityDto, string $propertyName): s

$fieldMapping = $entityDto->getClassMetadata()->getFieldMapping($propertyName);

return self::$doctrineTypeToFilterClass[$fieldMapping['type']] ?? TextFilter::class;
if (version_compare(InstalledVersions::getVersion('doctrine/orm'), '3.0', '<')) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix we use in other parts of the code:

// In Doctrine ORM 3.x, FieldMapping implements \ArrayAccess; in 4.x it's an object with properties
$fieldMapping = $associatedEntityDto->getClassMetadata()->getFieldMapping($propertyName);
// In Doctrine ORM 2.x, getFieldMapping() returns an array
/** @phpstan-ignore-next-line function.impossibleType */
if (\is_array($fieldMapping)) {
/** @phpstan-ignore-next-line cast.useless */
$fieldMapping = (object) $fieldMapping;
}
/** @phpstan-ignore-next-line function.alreadyNarrowedType */
$propertyDataType = property_exists($fieldMapping, 'type') ? $fieldMapping->type : $fieldMapping['type'];

It's a bit convoluted 😐 but it covers Doctrine ORM 2.x, 3.x and 4.x.

$index = $fieldMapping['type'];
} else {
$index = $fieldMapping->type;
}

return self::$doctrineTypeToFilterClass[$index] ?? TextFilter::class;
}
}