简体   繁体   中英

sonata sonata_type_collection display table

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?

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

You missed some options. Try this :

  ->add('eventImages', 'sonata_type_collection', array(
                'by_reference' => true,
                'label' => false,
                'type_options' => array('delete' => true),
                'cascade_validation' => true,
                'btn_add' => 'Add new EventImages',
                "required" => false ), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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