简体   繁体   English

Symfony Media捆绑软件-“无法在null变量上调用方法(“ id”)。”

[英]Symfony Media bundle - “Impossible to invoke a method (”id“) on a null variable.”

Environment 环境

Symfony 3.4.4 + FOSBundleUser + Sonata Admin + Media Bundle Symfony 3.4.4 + FOSBundleUser + Sonata Admin +媒体捆绑

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. 我在实体产品中创建了一个字段,以附加PDF格式的目录。

I have added in Sonata Adminel field mapping. 我已经在Sonata Adminel字段映射中添加了。

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: 在Sonata Admin中查看产品列表时,没有问题,但是在输入以编辑或创建新产品时,出现以下错误:

Impossible to invoke a method ("id") on a null variable. 无法对空变量调用方法(“ id”)。

This is the definition of my field to attach a PDF in my entity 这是在我的实体中附加PDF的字段定义

/**
 * @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 这是Sonata Admin表单中字段的映射

->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, 我遇到了同样的问题,但就我而言,我的项目是针对多语言站点的,因此每个实体都有其支持的翻译实体(使用Knp \\ DoctrineBehaviors),所以我所做的是。.我保持媒体映射(ORM)处于打开状态例如,两个站点都假定Product和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. 就我而言,我错过了将ORM导入Media实体类的过程。

use Doctrine\ORM\Mapping as ORM;

This was all it took to fix. 这就是所有需要解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM