I am trying to extend the SonataMediaBundle media class. I want to create my own media to add more relationships and properties.
But now I am stuck with this error:
Cannot invoke method ("id") on null variable in SonataDoctrineORMAdminBundle: Form: form_admin_fields.html.twig on line 59.
My Service Announcement:
services: gallery.admin.image: class: GalleryBundle\Admin\ImageAdmin arguments: [~, GalleryBundle\Entity\Image, SonataMediaBundle:MediaAdmin, @sonata.media.pool, @sonata.classification.manager.category] tags: - { name: sonata.admin, manager_type: orm, group: sonata_media, label_catalogue: SonataMediaBundle, label: Gallery} calls: - [ setModelManager, [@sonata.media.admin.media.manager]] - [ setTranslationDomain, [SonataMediaBundle]] - [ setTemplates, [[inner_list_row: SonataMediaBundle:MediaAdmin:inner_row_media.html.twig, outer_list_rows_mosaic: SonataMediaBundle:MediaAdmin:list_outer_rows_mosaic.html.twig, base_list_field: SonataAdminBundle:CRUD:base_list_flat_field.html.twig, list: SonataMediaBundle:MediaAdmin:list.html.twig, edit: SonataMediaBundle:MediaAdmin:edit.html.twig]]]
My admin class:
use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Form\FormMapper; use Sonata\ClassificationBundle\Model\CategoryManagerInterface; use Sonata\MediaBundle\Admin\BaseMediaAdmin; use Sonata\MediaBundle\Admin\ORM\MediaAdmin; use Sonata\MediaBundle\Provider\Pool; class ImageAdmin extends BaseMediaAdmin { public function __construct($code, $class, $baseControllerName, Pool $pool, CategoryManagerInterface $categoryManager) { parent::__construct($code, $class, $baseControllerName, $pool, $categoryManager); } protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Basic') ->add('tags', 'sonata_type_model', [ 'multiple' => true, ]) ->end() ; parent::configureFormFields($formMapper); } }
And my Essence:
use BlogBundle\Entity\PostTag; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Sonata\MediaBundle\Model\Media; class Image extends Media { private $id; protected $tags; public function __construct() { $this->tags = new ArrayCollection(); } public function getId() { return $this->id; } public function addTag(PostTag $tag) { $this->tags[] = $tag; return $this; } public function removeTag(PostTag $tag) { $this->tags->removeElement($tag); } public function getTags() { return $this->tags; } }
I know that the problem will be fixed using the next version. But with our current server we cannot update. The project is already in production, and I can not redo it.
I saw that the problem was that there would be more than one sonata_type_model in the administrator definition. But no matter how much I unlink PostTags, the error persists.
I do not know if the definition of the service is incorrect, or I forgot the event call or something like that.
I would appreciate any help. Thanks