gpt4 book ai didi

symfony - 尝试在空变量上扩展 SonataMediaBundle "Impossible to invoke a method ("id"时出错"

转载 作者:行者123 更新时间:2023-12-02 01:04:50 26 4
gpt4 key购买 nike

我正在尝试扩展 SonataMediaBundle 的媒体类。我想生成自己的媒体以添加更多关系和属性。

但现在我遇到了这个错误:

Impossible to invoke a method ("id") on a null variable in SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig at line 59.

我的服务声明是:

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]]]

我的管理类是:

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);
}
}

我的实体是:

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;
}
}

我知道下一个版本会解决这个问题。但是对于我们当前的服务器,我们无法升级。该项目已经投入生产,我无力重新制作它。

我已经看到问题是因为在管理员的定义中我会有多个 sonata_type_model。但无论我删除多少与 PostTags 的关系,错误仍然存​​在。

不知道是服务定义有误还是忘记了事件调用什么的。

如果有任何帮助,我将不胜感激。谢谢

最佳答案

我的建议是:

当您创建构建器时,使用 '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

并且您与 PostTag 之间存在多对多关系。

为什么不在 ImageAdmin 中使用“sonata_type_collection”而不是“sonata_type_model”?

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

希望对您有所帮助。

祝你好运!

关于symfony - 尝试在空变量上扩展 SonataMediaBundle "Impossible to invoke a method ("id"时出错",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com