Search code examples
phpsymfonysonata-adminsonata-media-bundle

Which admin_code to specify in sonata_type_collection?


In a Sonata project, I have a Page entity and a Media entity. My Page entity contains an images property that contains an ArrayCollection of Media entities. In my Page entity, I the following working code:

            ->add('galleries', 'sonata_type_collection', array(
                'label' => "Galleries",
                //'cascade_validation' => true,
                'required' => false,
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
            ))

... which gives me a nice inline form on my Page editing screen, allowing me to create and associate galleries inline.

But when I try this:

            ->add('images', 'sonata_type_collection', array(
                'label' => "Standalone Images",
                //'cascade_validation' => true,
                'required' => false,
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
            ))

... I find that I get a blank checkbox, with no form. (The page's images field contains an ArrayCollection of Media objects from the Sonata Media Bundle.)

I assume I need to use the admin_code option to specify an admin value. What string should I put in that option?

===

Edit #1:

By changing this:

                ->add('images', 'sonata_type_collection', array(
                    'label' => "Standalone Images",
                    'required' => false,
                ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))

... to this:

                ->add('images', 'sonata_type_collection', array(
                    'label' => "Standalone Images",
                    'required' => false,
                ), array())

... I am able to get a modal box to show up for creating new Media entities.

However, I then find that the new media entities do no show up on my Page editing screen. So this only partially fixes the problem.

===

Edit #2:

A bit of logging reveals that my Page entity's getImages() method is getting called, but that same entity's setImages() and addImage() methods are not getting called. I am not sure how to force the application to call these methods, nor how to make sure that the correct Media entities get fed to those methods.

===

Edit #3:

Here is the mapping in my Page.orm.xml file:

    <many-to-many field="images" target-entity="Application\Sonata\MediaBundle\Entity\Media">
        <cascade>
            <cascade-persist />
        </cascade>
        <join-table name="page_image">
            <join-columns>
                <join-column name="page_id" referenced-column-name="id" />
            </join-columns>
            <inverse-join-columns>
                <join-column name="image_id" referenced-column-name="id" unique="true" />
            </inverse-join-columns>
        </join-table>
    </many-to-many>

Solution

  • If you edit your collections inside another admin, you have to set the by_reference option to false. Thats for calling the setter method on the embedded objects. Especially on many to many relations its a common problem.