简体   繁体   中英

How to add a dynamic CrudController on a field Symfony/Doctrine

Situation:

I use EasyAdmin for the backend. I have an entity named Vehicle . From it, I've created Car and Bicycle .

I have another entity with a OneToMany relation linked to Vehicle . Problem is, when I click the link created by EasyAdmin, it redirects me to the admin page with the Vehicle crudId. I need it to redirect me to either to the Car or the Bicycle one, depending on the type of the Vehicle .

I've set the AssociationField like this:

public function configureFields(string $pageName): iterable
{
    return [
        AssociationField::new('Vehicle', 'The vehicle')->autocomplete()->setCrudController(Car::class)
    ]
}

Problem:

You can see that I set the crud controller to Car, but here is my problem: I need to set this dynamically, either Car or Bicycle. Is it possible to do it?

Thank you to everyone helping:)

So, you can create listener for controller event, and choose the right one and pass it like here https://symfony.com/doc/current/reference/events.html#kernel-controller

use Symfony\Component\HttpKernel\Event\ControllerEvent;

public function onKernelController(ControllerEvent $event)
{
    // ...

    // the controller can be changed to any PHP callable
    $event->setController($myCustomController);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM