Error trying to expand SonataMediaBundle "Unable to call method (" id ") for null variable"

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; /** * Image * * @ORM\Table(name="image") * @ORM\Entity(repositoryClass="GalleryBundle\Repository\ImageRepository") */ class Image extends Media { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\ManyToMany(targetEntity="BlogBundle\Entity\PostTag", inversedBy="gallery_images") */ protected $tags; public function __construct() { $this->tags = new ArrayCollection(); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Add tag. * * @param PostTag $tag * * @return Image */ public function addTag(PostTag $tag) { $this->tags[] = $tag; return $this; } /** * Remove tag. * * @param PostTag $tag */ public function removeTag(PostTag $tag) { $this->tags->removeElement($tag); } /** * Get tags. * * @return Collection */ 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

+3
source share
1 answer

My suggestions:

When you create your builder, use 'empty_on_new' => false.

empty_on_new (default is true): the associated data transformer returns null instead of an empty Media instance if binary content is not provided

https://sonata-project.org/bundles/media/3-x/doc/reference/form.html

And you have ManyToMany's attitude towards PostTags.

Why aren't you using sonata_type_collection in your ImageAdmin instead of sonata_type_model?

https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html#sonata-corebundle-form-type-collectiontype

Hope this helps you.

Good luck

+1
source

Source: https://habr.com/ru/post/1274928/


All Articles