Environment
Symfony 3.4.4 + FOSBundleUser + Sonata Admin + Media Bundle
Subject
I have installed Media Bundle. I have configured it following the steps indicated in the Official documentation.
I have created a field in my entity Products to attach a catalog in PDF format.
I have added in Sonata Adminel field mapping.
When viewing the list of products in Sonata Admin, there is no problem, but when enter to edit or create a new product, give this error:
Impossible to invoke a method ("id") on a null variable.
This is the definition of my field to attach a PDF in my entity
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="fileCatalog_id", referencedColumnName="id")
* })
*/
private $fileCatalog;
This is the mapping of the field in the Sonata Admin form
->add('fileCatalog', 'sonata_type_model_list', array(
'required' => false,
'label'=>'Imagen Español'
), array(
'link_parameters' => array(
'context' => 'default',
'provider' => 'sonata.media.provider.file',
'empty_on_new' => true,
)
)
)
I had the same issue, but in my case, my project is for a multi language site therefore each entities having its supporting Translation entities (using Knp\\DoctrineBehaviors), so what I did was.. I keep the media mapping (ORM) on both site as for example assume Product and ProductTranslation,
namespace Website\ProductBundle\Entity;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
class Product implements TranslatableInterface
{
use ORMBehaviors\Translatable\Translatable;
......
......
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
* @ORM\JoinColumn(referencedColumnName="id", onDelete="SET NULL", nullable=true)
*/
protected $image;
}
namespace Website\ProductBundle\Entity;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
class ProductTranslation
{
use ORMBehaviors\Translatable\Translation;
......
......
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
* @ORM\JoinColumn(referencedColumnName="id", onDelete="SET NULL", nullable=true)
*/
protected $image;
}
And thats all I was able to rid off the problem. and it works fine.
In my case I had missed importing ORM into the Media entity class.
use Doctrine\ORM\Mapping as ORM;
This was all it took to fix.
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.