sonata sonata_type_collection display table

manuel picture manuel · Feb 19, 2015 · Viewed 9.5k times · Source

I have two entities: "Event" and "EventImage". One event can have multiple images.

This is the relationship defined on the Event table:

**
* Event
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\EventRepository")
*/

class Event
{

    /**
     * @ORM\OneToMany(targetEntity="EventImage", mappedBy="event")
     */
    protected $eventImages;

}

and this is the relationship defined on the EventImage table:

/**
 * EventImage
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\EventImageRepository")
 */
class EventImage
{
    /**
     * @ORM\ManyToOne(targetEntity="Event", inversedBy="eventImages")
     * @ORM\JoinColumn(name="event_id", referencedColumnName="id")
     */
    protected $event;
}

and in my "configureFormFields" in EventAdmin

$formMapper
    ->add('eventImages', 'sonata_type_collection',array(), array(
                        'edit' => 'inline',
                        'inline' => 'standard',
                        'sortable'  => 'listOrder'
                ))

and in my "" in EventImageAdmin

$formMapper
    ->add('id')
    ->add('imagePath', 'text')
->end()
;

Now I saw some example around where you get a nice formatted table, with each pulled record in a row, with a checkbox to delete the row and also the dragging option, and also the "add a new row" button to link a new element(or add a new one)

But all i got is a cascade list of associated eventImage records, not formatted in a table, with no "add a new row" option.

What am I doing wrong?

Answer

M Khalid Junaid picture M Khalid Junaid · Feb 23, 2015

In your EventImageAdmin there is no need of id field also use sonata_type_model_list for imagePath

  $formMapper
        ->add('imagePath', 'sonata_type_model_list', array('required' => false));

In your EventAdmin define admin_code service id of EventImageAdmin in fourth parameter of $formMapper's add() function

$formMapper
    ->add( 'eventImages', 'sonata_type_collection', array(
            'cascade_validation' => false,
            'type_options'       => array( 'delete' => false ),
        ), array(

            'edit'            => 'inline',
            'inline'          => 'table',
            'sortable'        => 'position',
            'link_parameters' => array( 'context' => 'define context from which you want to select media or else just add default' ),
            'admin_code'      => 'sonata.admin.your_service_id_here'
            /*here provide service name for junction admin */
        )
    );

For more see my another answer regarding Handling multiple file uploads in Sonata Admin Bundle