简体   繁体   English

通过sonata_type_collection字段将sonata_media_type用作1:N时出现500错误

[英]500 error when using sonata_media_type as a 1:N via a sonata_type_collection field

I'm using SonataAdminBundle to administer the backend of a project I'm working on. 我正在使用SonataAdminBundle来管理我正在进行的项目的后端。 In this case I'm wanting to add one-or-more images to "items". 在这种情况下,我想要向“项目”添加一个或多个图像。 Here's the relevant bits of the entity 这是实体的相关位

src/My/Bundle/Entity/Item.php SRC /我/包/实体/ Item.php

/**
 * Item
 *
 * @ORM\Table(name="item")
 * @ORM\Entity()
 */
class Item
{
  /**
   * @var Media
   *
   * @ORM\OneToMany(targetEntity="\Application\Sonata\MediaBundle\Entity\Media", mappedBy="item")
   * ORM\JoinTable(name="item_media",
   *     joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")}
   *   , inverseJoinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id", unique=true)}
   * )
   */
  protected $media;
}

And now the relevant bits of the admin class: 现在管理类的相关部分:

src/My/Bundle/Entity/Item.php SRC /我/包/实体/ Item.php

class ItemAdmin extends Admin
{
  protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
        ->add('media', 'sonata_type_collection'
        , array(
              'required' => false
            , 'type' => 'sonata_media_type'
            , 'by_reference' => false
            , 'type_options' => array(
                  'provider' => 'sonata.media.provider.image'
                , 'context'  => 'default'
                , 'auto_initialize' => false
              )
          )
        , array(
              'edit' => 'inline'
            , 'inline' => 'table'
            , 'allow_delete' => true
            , 'sortable' => 'position'
        ))
    ;
  }
}

Now in the UI itself, when I click the + Add New button, the AJAX response is a 500 error with the following message: 现在在UI本身,当我单击+ Add New按钮时,AJAX响应是500错误,带有以下消息:

Impossible to invoke a method ("trans") on a NULL variable ("") in SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig at line 30 无法在SonataDoctrineORMAdminBundle中的NULL变量(“”)上调用方法(“trans”):第30行的CRUD:edit_orm_one_to_many.html.twig

I'm not really sure where to go from here. 我不确定从哪里开始。 I don't have much experience with the SonataAdminBundle, espeically with this type of inline-editing/creation of other entities. 我对SonataAdminBundle没有多少经验,特别是这种类型的内联编辑/创建其他实体。

Version Info: 版本信息:

  • PHP 5.4.19 PHP 5.4.19
  • Symfony 2.3.4 Symfony 2.3.4
  • SonataAdminBundle 2.2.3 SonataAdminBundle 2.2.3
  • SonataMediaBundle 2.2.3 SonataMediaBundle 2.2.3

This issue seems to be related to this question in the sonata-users google group. 这个问题似乎与sonata-users google group中的这个问题有关。

The problem seems to be that the media-type's label isn't passed to the template which results in an error when invoking the translator/calling the trans method in the template here: 问题似乎是媒体类型的标签没有传递给模板,这导致在调用模板中的翻译器/调用trans方法时出错:

{{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}

in SonataDoctrineORMAdminBundle/Resources/views/CRUD/edit_orm_one_to_many.html.twig . SonataDoctrineORMAdminBundle / Resources / views / CRUD / edit_orm_one_to_many.html.twig中

I suggest you first try to add a label to your form-type. 我建议你首先尝试在表单类型中添加标签。

'label' => 'Media' 

If that doesn't work you could fork the repository and clone it afterwards: 如果这不起作用,您可以分叉存储库并在之后克隆它:

git clone https://github.com/yourusername/SonataDoctrineORMAdminBundle.git

then create a new branch using the 2.2.3 tag: 然后使用2.2.3标记创建一个新分支:

git branch bugfix-2.2.3 2.2.3
git checkout bugfix-2.2.3

now change line 30 in edit_orm_one_to_many.html.twig to (or remove it completely which would result in the labels not being rendered at all) 现在将edit_orm_one_to_many.html.twig line 30更改为(或完全删除它,这将导致标签根本不被渲染)

{% if nested_field.vars.label %}
    {{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}
{% endif %}

Afterwards commit your changes and ... 然后提交您的更改并...

... either overwrite the tag (please note that tags should normally never be overwritten ) ...要么覆盖标签(请注意标签通常不会被覆盖)

git commit
git tag -f 2.2.3

... or ( better ) create an alias in your composer.json ...或者( 更好 )在composer.json创建一个别名

"require": {
    "sonata/doctrine-orm-admin-bundle" : "bugfix-2.2.3#<commit-sha> as 2.2.3"

then add this to your composer.json : 然后将其添加到您的composer.json

"repositories": [
    { "type": "vcs", "url": "http://github.com/yourusername/doctrine-orm-admin-bundle"}
],

afterwards run 然后跑

composer update sonata/doctrine-orm-admin-bundle

I'm not sure if it helps but the many-to-many support for inline-editing has been added in this commit . 我不确定它是否有帮助,但是在此提交中添加了对内联编辑的多对多支持。 You could try changing your association to many-to-many and test wether the exception disappears. 您可以尝试将关联更改为多对多,并测试异常消失。

If that doesn't work either please report back and i will dig deeper into this. 如果这不起作用,请报告回来,我会深入研究。

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

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