Question

I'm using SonataAdminBundle to administer the backend of a project I'm working on. 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

/**
 * 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

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:

Impossible to invoke a method ("trans") on a NULL variable ("") in SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig at line 30

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.

Version Info:

  • PHP 5.4.19
  • Symfony 2.3.4
  • SonataAdminBundle 2.2.3
  • SonataMediaBundle 2.2.3
Was it helpful?

Solution

This issue seems to be related to this question in the 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:

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

in 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:

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)

{% 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

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

then add this to your 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top