I'm trying to extend the media class of SonataMediaBundle. I want to generate my own media to add more relations and properties.
But now I am stuck with this error:
Impossible to invoke a method ("id") on a null variable in SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig at line 59.
My service declaration is:
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 is:
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 Entity is:
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 would be fixed using the next version. But with our current server we cannot upgrade. The project is already on production, and I can't afford to remake it.
I have seen that the problem was because I would have more than one sonata_type_model
, in the definition of the admin. But no matter how much I delete the relation with PostTags the error persists.
I don't know if the service definition is wrong or I have forgotten an event call or something.
I would appreciate any help. Thanks
My suggestions are:
When you are creating your builder, use 'empty_on_new' => false.
empty_on_new (default is true): the related data transformer will return null instead of an empty Media instance if no binary content is provided
https://sonata-project.org/bundles/media/3-x/doc/reference/form.html
And you have a ManyToMany relation with PostTags.
Why doesn't you use 'sonata_type_collection' in your ImageAdmin instead of 'sonata_type_model' ?
I hope it will help you.
Good luck !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.