EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.06k stars 1.02k forks source link

Embedded associations break filters #4778

Open Naroh091 opened 2 years ago

Naroh091 commented 2 years ago

Describe the bug Adding an embedded association to CRUD controllers break the ability of adding filters to the controller.

To Reproduce

Classes: Donation, Subscription, and User.

Donation has a ManyToOne relationship with Subscription. Subscription has an OneToOne subscription with User.

In the Donation CRUD controller I'm showing the associated User this way:

yield AssociationField::new('subscription.user', 'Usuario')->setCrudController(User::class);

So far, this works fine.

Then, if I add a simple NumericFilter to show the amount of the donation:

 public function configureFilters(Filters $filters): Filters
    {
        return $filters
            ->add(NumericFilter::new('amount', 'Cantidad'));
    }

I get the following error when I open the filters modal:

image

The issue is that in AssociationConfigurator.php, line 82, the instance that should be returned with $entityDto->getInstance() is null.

If I remove the AssociationField then that part of the code that checks for embedded association doesn't get executed and everything works fine.

Happening with the latest version of EasyAdmin, v3.5.10

Naroh091 commented 2 years ago

Seems like surrounding the lines referring to the instance with a try/catch block does the trick:

            try {
                $relatedEntityId = $accessor->getValue($entityDto->getInstance(), $propertyName . '.' . $metadata->getIdentifierFieldNames()[0]);
                $relatedEntityDto = $this->entityFactory->create($targetEntityFqcn, $relatedEntityId);

                $field->setFormTypeOptionIfNotSet('class', $relatedEntityDto->getFqcn());

                $field->setCustomOption(AssociationField::OPTION_RELATED_URL, $this->generateLinkToAssociatedEntity($targetCrudControllerFqcn, $relatedEntityDto));
                $field->setFormattedValue($this->formatAsString($relatedEntityDto->getInstance(), $relatedEntityDto));
            } catch (\Exception $e) {

            }

But surely there's a better approach to fix the issue.

pkly commented 2 years ago

@Naroh091 I made a PR with the embedded associations and now made a fix for this #4786, though granted it's basically the same, just more specific. Sorry for the trouble, I only found it today - I wasn't able to fully test it in our live app enough and I guess it broke a few things.