EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext::getEntity(): Return value must be of type EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto, null returned

Open treeindark opened this issue 9 months ago • 11 comments

Describe the bug In BeforeCrudActionEvent on new Entity

$context->getEntity() is null

To Reproduce

public static function getSubscribedEvents(): array
 {
     return [
         BeforeCrudActionEvent::class => ['checkUser'],
     ];
 }

 public function checkUser(BeforeCrudActionEvent $event): void
 {
     $context = $event->getAdminContext();
     if ($context) {
         $entity = $context->getEntity()->getInstance();
...

(OPTIONAL) Additional context IN final class AdminContext implements AdminContextInterface private ?EntityDto $entityDto; <-nullable

public function __construct(Request $request, ?UserInterface $user, I18nDto $i18nDto, CrudControllerRegistry $crudControllers, DashboardDto $dashboardDto, DashboardControllerInterface $dashboardController, AssetsDto $assetDto, ?CrudDto $crudDto, ?EntityDto $entityDto, ?SearchDto $searchDto, MenuFactoryInterface $menuFactory, TemplateRegistry $templateRegistry, bool $usePrettyUrls = false) <-entityDTO nullable

public function getEntity(): EntityDto <- NOT NULLABLE

Is it normal ?

treeindark avatar Feb 27 '25 13:02 treeindark

Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.

For me, the solution was to add a #[AdminAction('{entityId}/action', 'admin_entity_action')] attribute to the method that handles the CRUD action.

(FTR, a concrete example for a refresh action on a News entity would be #[AdminAction('{entityId}/refresh', 'admin_news_refresh')].)

fwg avatar Feb 28 '25 21:02 fwg

Yes, is a custom action if i use adminAction , i no more use Admin context, and I load entity in action wihout it.

    #[AdminAction('{entityId}/reSendEmail', 'admin_resend_action')]
    public function reSendEmail( AdminContext $context,
        #[MapEntity(mapping: ['entityId' => 'id'])] Process $process
    ): \Symfony\Component\HttpFoundation\RedirectResponse {
        //     $process = $context->getEntity()->getInstance();


treeindark avatar Mar 03 '25 08:03 treeindark

Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.

I have the same issue.

To reproducer:

  1. enable beautiful URLs
  2. Declare a custome action and it's controller
  3. Click on the link
  4. 💥

lyrixx avatar Mar 07 '25 14:03 lyrixx

same issue and @fwg workaround worked

allan-simon avatar Apr 03 '25 12:04 allan-simon

I did @fwg workaround and still not working, following this for export with filters used to work https://symfonycasts.com/screencast/easyadminbundle/global-action but not it gives Call to a member function getFiltersConfig() on null

Any other tip to look to fix it ?

After messing around a little more the changes I made is putting the AdminAction and change the link to this: ->linkToCrudAction('export')

Now is working.

rsantellan avatar Apr 08 '25 01:04 rsantellan

I did @fwg workaround and still not working, following this for export with filters used to work https://symfonycasts.com/screencast/easyadminbundle/global-action but not it gives Call to a member function getFiltersConfig() on null

Any other tip to look to fix it ?

After messing around a little more the changes I made is putting the AdminAction and change the link to this: ->linkToCrudAction('export')

Now is working. Call to a member function getFiltersConfig() on null It appears to be the same issue. Could you please explain the solution in more detail? Thank you!

vaigtech avatar Apr 12 '25 16:04 vaigtech

Same but different here. I just:

  1. overridden the new action of a crud controller
  2. added a "new foo" button on another page using this:
    $newFooAction = Action::new('newFoo', 'New foo')
            ->linkToRoute('admin_foo_new', function (Organization $org) {
                return ['id' => $org->getId()];
            });
    
  3. ❌ The linkToRoute uses to an old-formatted route
  4. ❌ The entity is empty in the context of my "new" route

Nek- avatar Jun 10 '25 19:06 Nek-

Same here.

1/ https://localhost/easy-admin/foo-bar works

2/ https://localhost/easy-admin/foo-bar works but https://localhost/easy-admin?crudAction=index&crudControllerFqcn=App%5CController%5CAdmin%5CFooBarCrudController won't work

But MenuItem::linkToCrud generate those links...

3/ For some reasons

MenuItem::linkToRoute('FooBar', 'fas fa-list', 'easy_admin_foo_bar_index');

generates https://localhost/easy-admin?routeName=easy_admin_foo_bar_index that has the same problem as OP.

while $this->redirectToRoute('easy_admin_foo_bar_index'); works.

conradfr avatar Jul 23 '25 18:07 conradfr

Issue still persists, I suggest moving to version 4.20.2

Elemtro avatar Sep 23 '25 13:09 Elemtro

Is this a custom CRUD action? Because I had the same error trying to invoke one of my custom actions, under the new pretty URLs routing.

For me, the solution was to add a #[AdminAction('{entityId}/action', 'admin_entity_action')] attribute to the method that handles the CRUD action.

(FTR, a concrete example for a refresh action on a News entity would be #[AdminAction('{entityId}/refresh', 'admin_news_refresh')].)

In last versions we must now use AdminRoute instead of AdminAction.

For eg.

use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminRoute;

#[AdminRoute(path: '/moveUp', name: 'move_up')]
public function moveUp(AdminContext $context): Response
{
    return $this->move($context, Direction::Up);
}

MonsieurV avatar Oct 22 '25 19:10 MonsieurV

Same issue without any action. Just switched to pretty URLs and my browser still remember old URLs and I stumbled upon the same error.

While looking at it, isn't there a fundamental bug between constructor arg and getter?

https://github.com/EasyCorp/EasyAdminBundle/blob/f718248b96fbff1d1dd4d38acaaa2c25af52e609/src/Context/AdminContext.php#L45

https://github.com/EasyCorp/EasyAdminBundle/blob/f718248b96fbff1d1dd4d38acaaa2c25af52e609/src/Context/AdminContext.php#L82-L85

private readonly ?EntityDto $entityDto takes a nullable type whereas the getter expect an EntityDto?

nlemoine avatar Nov 05 '25 10:11 nlemoine