Error "Impossible to invoke a method ("id") on a null variable" when loading ManyToOne with sonata_type_model_list through sonata_type_admin

479 views
Skip to first unread message

nrodr...@tupaca.com

unread,
Jun 2, 2016, 9:34:31 AM6/2/16
to sonata-users

Hi all,

I've a problem when trying to load a sonata_type_model_list field within a sonata_type_admin. It gives me the following error:
Impossible to invoke a method ("id") on a null variable in SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig at line 60

My entities:
  • AppBundle\Entity\Page - it have a OneToOne to Content
  • AppBundle\Entity\Content\Content (abstract)
  • AppBundle\Entity\Content\ContentText extends Content
  • AppBundle\Entity\Content\ContentImage extends Content - it have a ManyToOne to media-bundle's "Media"
  • AppBundle\Entity\Content\ContentVideo extends Content - it have a ManyToOne to media-bundle's "Media"

My admin classes:
  • AppBundle\Admin\PageAdmin - for AppBundle\Entity\Page
  • AppBundle\Admin\ContentAdmin - for AppBundle\Entity\Content\Content -  from configureFormFields it loads the needed fields for each "Content" subclass (text for text, and sonata_type_model_list for medias)


Details of the error:

From ContentAdmin
Everything works fine, and I'm able to edit everything, using sonata_type_model_list

From PageAdmin
When I load the related (OneToOne) Content using sonata_type_admin:
  • if the content related is a ContentText , it works fine. (Actually, I've more Content subclasses similar to ContentText, and it works fine. But none of them have ManyToOne fields)
  • If the content related is a ContentImage or ContentVideo (both loaded with sonata_type_model_list), it fires the error:

Impossible to invoke a method ("id") on a null variable in SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig at line 60


My versions:

  • sonata-project/admin-bundle 2.3.10
  • sonata-project/doctrine-orm-admin-bundle 2.3.4
  • sonata-project/media-bundle 2.3.4


I've read similar errors on github, but I'm not sure if they are related. Just for your info, I copy them:

Thanks!!

nrodr...@tupaca.com

unread,
Jun 2, 2016, 11:37:53 AM6/2/16
to sonata-users
I've some news:
It seems that the problem occurs only if I use inheritance on Content... If I just use a relation from Page to ContentImage, it works fine.

nrodr...@tupaca.com

unread,
Jun 2, 2016, 3:54:28 PM6/2/16
to sonata-users
More data:
The problem just occurs if I load more than one sonata_type_admin per form, and one of them has ManyToOne relation.

Abhishek Niraula

unread,
Oct 21, 2019, 7:26:07 AM10/21/19
to sonata-users
I had missed use Doctrine\ORM\Mapping as ORM;
Adding this line in Media entity fixed the issue for me. And my current Entity structure is same as discussed in the previous comment
Reply all
Reply to author
Forward
0 new messages