diff --git a/config/services.php b/config/services.php index 8d4fa3f36f..31280a28be 100644 --- a/config/services.php +++ b/config/services.php @@ -373,6 +373,7 @@ ->arg(2, service('request_stack')) ->arg(3, service(ControllerFactory::class)) ->arg(4, new Reference(FieldFactory::class)) + ->arg(5, service(EntityRepository::class)) ->set(AvatarConfigurator::class) diff --git a/src/Field/Configurator/AssociationConfigurator.php b/src/Field/Configurator/AssociationConfigurator.php index 0d8c5d9f97..3c52f7e395 100644 --- a/src/Field/Configurator/AssociationConfigurator.php +++ b/src/Field/Configurator/AssociationConfigurator.php @@ -20,6 +20,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField; use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudAutocompleteType; use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType; +use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository as EAEntityRepository; use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; @@ -38,6 +39,7 @@ public function __construct( private RequestStack $requestStack, private ControllerFactory $controllerFactory, private FieldFactory $fieldFactory, + private EAEntityRepository $entityRepository, ) { } @@ -49,6 +51,11 @@ public function supports(FieldDto $field, EntityDto $entityDto): bool public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void { $propertyName = $field->getProperty(); + $resolvedProperty = $this->entityRepository->resolveNestedAssociations(null, $entityDto, $propertyName, true); + /** @var EntityDto $entityDtoResolved */ + $entityDtoResolved = $resolvedProperty['entity_dto']; + /** @var string $resolvedProperty */ + $resolvedProperty = $resolvedProperty['property_name']; if (!$this->isAssociation($entityDto->getClassMetadata(), $propertyName)) { throw new \RuntimeException(sprintf('The "%s" field is not a Doctrine association, so it cannot be used as an association field.', $propertyName)); @@ -56,7 +63,7 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c // the target CRUD controller can be NULL; in that case, field value doesn't link to the related entity $targetCrudControllerFqcn = $field->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER) - ?? $context->getAdminControllers()->findCrudControllerByEntity($entityDto->getClassMetadata()->getAssociationTargetClass($propertyName)); + ?? $context->getAdminControllers()->findCrudControllerByEntity($entityDtoResolved->getClassMetadata()->getAssociationTargetClass($resolvedProperty)); if (true === $field->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM)) { if (false === $entityDto->getClassMetadata()->isSingleValuedAssociation($propertyName)) {