EasyCorp / EasyAdminBundle

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

Adding custom crud action on the NEW page generates an invalid URL to it #6474

Open speller opened 3 weeks ago

speller commented 3 weeks ago

Describe the bug When I add a custom action to the NEW page, I'm getting the entity with "id = 0" does not exist in the database error when trying to access it.

To Reproduce

    public function configureActions(Actions $actions): Actions
    {
        return parent::configureActions($actions)
            ->add(
                Crud::PAGE_NEW,
                Action::new('foo', 'Foo')->linkToCrudAction('foo')
            )
        ;
    }

(OPTIONAL) Additional context

The "App\Entity\Foo" entity with "id = 0" does not exist in the database. The entity may have been deleted by mistake or by a "cascade={"remove"}" operation executed by Doctrine.

Stack:

in vendor/easycorp/easyadmin-bundle/src/Factory/EntityFactory.php (line 149)
in vendor/easycorp/easyadmin-bundle/src/Factory/EntityFactory.php -> getEntityInstance (line 111)
in vendor/easycorp/easyadmin-bundle/src/Factory/EntityFactory.php -> doCreate (line 70)
in vendor/easycorp/easyadmin-bundle/src/Factory/AdminContextFactory.php -> create (line 244)
in vendor/easycorp/easyadmin-bundle/src/Factory/AdminContextFactory.php -> getEntityDto (line 62)
in vendor/easycorp/easyadmin-bundle/src/EventListener/AdminRouterSubscriber.php -> create (line 78)
in vendor/symfony/event-dispatcher/Debug/WrappedListener.php -> onKernelRequest (line 116)
in vendor/symfony/event-dispatcher/EventDispatcher.php -> __invoke (line 206)
in vendor/symfony/event-dispatcher/EventDispatcher.php -> callListeners (line 56)
in vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php -> dispatch (line 127)
in vendor/symfony/http-kernel/HttpKernel.php -> dispatch (line 154)
in vendor/symfony/http-kernel/HttpKernel.php -> handleRaw (line 76)

The URL that is generated for the action: http://localhost:7000/dashboard?crudAction=foo&crudControllerFqcn=App%5CController%5CFooCOntroller&entityId=0

damien-louis commented 3 days ago

linkToCrudAction seems to be related to action on a specific row of your entity. You have to disable new button and create another new button to call your custom controller (with route named backoffice_user_create for example):


    public function configureActions(Actions $actions): Actions
    {

        $customNewAction = Action::new('customNew', 'Add New')
            ->linkToRoute('backoffice_user_create')->addCssClass('btn btn-primary')->createAsGlobalAction()
        ;

        return $actions
            ->add(Crud::PAGE_INDEX, $customNewAction)
           ->disable(Action::NEW)
        ;
    }