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

时间:2017-11-06 23:52:52

标签: symfony sonata-admin

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

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

并在GalleryMedia Entitiy中

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

我如何以表格

执行此操作

1 个答案:

答案 0 :(得分:2)

  1. 一对多:sonata_type_collection
  2. 在你的情况下:

           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
    2. 你可以用学说制作多对多:

      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
               ])