Skip to content

Commit cf67629

Browse files
committed
Adds a fix for nested properties on association fields
Only merge AFTER EasyCorp#7500 is merged
1 parent 8462640 commit cf67629

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
->arg(2, service('request_stack'))
374374
->arg(3, service(ControllerFactory::class))
375375
->arg(4, new Reference(FieldFactory::class))
376+
->arg(5, service(EntityRepository::class))
376377

377378
->set(AvatarConfigurator::class)
378379

src/Field/Configurator/AssociationConfigurator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
2121
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudAutocompleteType;
2222
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
23+
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository as EAEntityRepository;
2324
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
2425
use Symfony\Component\HttpFoundation\RequestStack;
2526
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
@@ -38,6 +39,7 @@ public function __construct(
3839
private RequestStack $requestStack,
3940
private ControllerFactory $controllerFactory,
4041
private FieldFactory $fieldFactory,
42+
private EAEntityRepository $entityRepository,
4143
) {
4244
}
4345

@@ -49,14 +51,19 @@ public function supports(FieldDto $field, EntityDto $entityDto): bool
4951
public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void
5052
{
5153
$propertyName = $field->getProperty();
54+
$resolvedProperty = $this->entityRepository->resolveNestedAssociations(null, $entityDto, $propertyName, true);
55+
/** @var EntityDto $entityDtoResolved */
56+
$entityDtoResolved = $resolvedProperty['entity_dto'];
57+
/** @var string $resolvedProperty */
58+
$resolvedProperty = $resolvedProperty['property_name'];
5259

5360
if (!$this->isAssociation($entityDto->getClassMetadata(), $propertyName)) {
5461
throw new \RuntimeException(sprintf('The "%s" field is not a Doctrine association, so it cannot be used as an association field.', $propertyName));
5562
}
5663

5764
// the target CRUD controller can be NULL; in that case, field value doesn't link to the related entity
5865
$targetCrudControllerFqcn = $field->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER)
59-
?? $context->getAdminControllers()->findCrudControllerByEntity($entityDto->getClassMetadata()->getAssociationTargetClass($propertyName));
66+
?? $context->getAdminControllers()->findCrudControllerByEntity($entityDtoResolved->getClassMetadata()->getAssociationTargetClass($resolvedProperty));
6067

6168
if (true === $field->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM)) {
6269
if (false === $entityDto->getClassMetadata()->isSingleValuedAssociation($propertyName)) {

0 commit comments

Comments
 (0)