奏鸣曲管理一对多,多对多形式

问题描述 投票:1回答:1

我有一个存储在galleries_media中的OneToMany相关实体库和媒体,我在GalleryMedia Entitiy中有多对一相关和多对一相关

/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="\Entity\GalleryMedia", mappedBy="gallery", cascade={"persist", "remove"})
*/
protected $galleriesMedia;

并在Gallery Media Entity中

/**
 * @ORM\ManyToOne(targetEntity="\Entity\Gallery", inversedBy="galleriesMedia")
 * @ORM\JoinColumn(name="gallery_id", referencedColumnName="id", nullable=false)
 */
protected $gllary;

/**
 * @ORM\ManyToOne(targetEntity="\Entity\Media", inversedBy="galleries")
 * @ORM\JoinColumn(name="media_id", referencedColumnName="id", nullable=false)
 */
protected $media;

我是怎么做到这一点的

symfony sonata-admin
1个回答
2
投票
  1. 一对多:sonata_type_collection

在你的情况下:

       class GalleryAdmin 
       {
           // ...

            $formMapper    
                ->add('galleriesMedia', 'sonata_type_collection', [
                    'required'      => false,
                    'label'         => 'my_galleries_media_label',   
                    'btn_add'       => 'my_add_button_name',
                    'type_options'  => [
                        'delete' => false,
                    ],
                ], [
                    'edit'          => 'inline', // or standard
                    'inline'        => 'table',  // or standard
                    'sortable'      => 'id',     // by any field in your entity
                    'limit'         => 5,        // you can remove this - this is a limit of items
                    'allow_delete'  => false, 
                    'placeholder'   => $this->trans('admin.placeholder.no_media'), 
                ])
            ;

       class GalleriesMediaAdmin 
       {
            // ...

           $formMapper 
               ->add('media', 'sonata_type_model_list', [
                   'required'      => true,
                   'btn_add'       => false,
                   'btn_list'      => 'name_of_list_button'
                   'btn_delete'    => false,     
                   'btn_catalogue' => 'admin', 
                   'label'         => 'name_of_your_label',
               ], [
                   'placeholder'   => 'choose media',
                   'edit'      => 'inline',
                   'inline'    => 'table',
                   'sortable'  => 'id',
               ])
  1. 多对多:sonata_type_model

你可以用学说制作多对多:

class Gallery 
{
    // ...

     /**
     * Unidirectional Many-To-Many ()
     *
     * Every Gallery can have a lot of medias 
     *
     * @ORM\ManyToMany(targetEntity="\Entity\Media")
     * @ORM\JoinTable(
     *      name="gallery_media_table",
     *      joinColumns={@ORM\JoinColumn(name="gallery_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="media_id", referencedColumnName="id", onDelete="CASCADE")}
     *      )
     *
     * @Assert\Valid()
     */
     protected $medias;

然后在您的GalleryAdmin中,您应该使用sonata_type_model:

     $formMapper
         ->add('medias', 'sonata_type_model', [
             'multiple'              => true,
             'expanded'              => true,     // or false
             'class'                 => Media::class,
             'property'              => 'name',   // or any field in your media entity
             'label'                 => 'your_label',
             'btn_add'               => true,
             'btn_list'              => false,
             'btn_delete'            => true,
             'btn_catalogue'         => 'admin',   // or your own translate catalogue in my case file admin.en.yml
         ])

推荐问答