duanlin1931 2018-06-28 23:32
浏览 73
已采纳

Symfony 4中的Sonata Media Bundle模板

I have installed Sonata Media Bundle in Symfony 4 and all is correct, but something is different respect t Symfony 3.

I can't see the service in Sonata Admin and when I add the sonata media bundle field to an Admin Class this shows a different template.

Here images:

Sonata Media Bundle template - Symfony 4, in User Entity

Sonata Media Bundle template - Symfony 3, in User Entity

Sonata Media Bundle template - Symfony 3, Adding new image

As you can see the template is not working in Symfony 4 or I'm missing something in my code.

My Sonata Media config

sonata_media.yaml

    sonata_media:
            class:
                media: App\Application\Sonata\MediaBundle\Entity\Media
                gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
                gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
            default_context: default
            contexts:
                default:
                    providers:
                        - sonata.media.provider.dailymotion
                        - sonata.media.provider.youtube
                        - sonata.media.provider.image
                        - sonata.media.provider.file
                        - sonata.media.provider.vimeo

                    formats:
                        small: { width: 100 , quality: 70}
                        big:   { width: 500 , quality: 70}

            cdn:
                server:
                    path: /upload/media

            filesystem:
                local:
                    # Directory for uploads should be writable
                    directory: "%kernel.project_dir%/public/upload/media"
                    create: false


            providers:
                # ...
                file:
                    # the resizer must be set to false, otherwhise this can delete icon files from the fs
                    resizer:    false
                image:
                   thumbnail: sonata.media.thumbnail.format          # default value
        #           thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
        #            thumbnail: sonata.media.thumbnail.liip_imagine    # use the LiipImagineBundle to resize the image
                vimeo:
                    thumbnail: sonata.media.thumbnail.format          # default value
        #           thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
        #           thumbnail: sonata.media.thumbnail.liip_imagine    # use the LiipImagineBundle to resize the image
                youtube:
                    thumbnail: sonata.media.thumbnail.format          # default value
        #           thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
        #           thumbnail: sonata.media.thumbnail.liip_imagine    # use the LiipImagineBundle to resize the image
                dailymotion:
                    thumbnail: sonata.media.thumbnail.format          # default value
        #           thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
        #           thumbnail: sonata.media.thumbnail.liip_imagine    # use the LiipImagineBundle to resize the image

My User's Admin Class

    // src/Admin/OgaUsersAdmin.php
    namespace App\Admin;

    use Sonata\AdminBundle\Admin\AbstractAdmin;
    use Sonata\AdminBundle\Datagrid\ListMapper;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\MediaBundle\Form\Type\MediaType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;

    class OgaUsersAdmin extends AbstractAdmin
    {
        protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper->add('userFirstName', TextType::class)
                       ->add('userCollection', MediaType::class, array(
                             'provider' => 'sonata.media.provider.image',
                             'context'  => 'default'
                            ));

        }

        protected function configureDatagridFilters(DatagridMapper $datagridMapper)
        {
            $datagridMapper->add('userFirstName');
        }

        protected function configureListFields(ListMapper $listMapper)
        {
            $listMapper->addIdentifier('userFirstName');
        }
    }

My Users Entity and Media Bundle field

    namespace App\Entity;

    use Application\Sonata\MediaBundle\Entity\Media;
    use Doctrine\ORM\Mapping as ORM;

    /**
     * OgaUsers
     *
     * @ORM\Table(name="oga_users", indexes={@ORM\Index(name="memb_id_idx", columns={"memb_id"}), @ORM\Index(name="comp_id_idx", columns={"comp_id"}), @ORM\Index(name="u_ui_id_idx", columns={"user_collection"})})
     * @ORM\Entity
     */
    class OgaUsers
    {
        /**
         * @var int
         *
         * @ORM\Column(name="user_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $userId;

        /**
         * @var Media
         *
         * @ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Media")
         * @ORM\JoinColumns({
         *     @ORM\JoinColumn(name="userCollection", referencedColumnName="id")
         * })
         */
       private $userCollection;

Getter and Settter

public function getUserCollection(): ?\App\Application\Sonata\MediaBundle\Entity\Media
{
    return $this->userCollection;
}

public function setUserCollection(?\App\Application\Sonata\MediaBundle\Entity\Media $userCollection): self
{
    $this->userCollection = $userCollection;

    return $this;
}

Thank's

  • 写回答

1条回答 默认 最新

  • dongyu7074 2018-07-12 23:38
    关注

    After search for some weeks.

    I have the answer.

    First I need to add this line to my sonata_media.yaml, to see the Media Library in Admin Dashboard.

    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
    

    After in Admin in configureFormFields, just need to add ModellistType::class, to the media field.

    class OgaUsersAdmin extends AbstractAdmin
    {
        protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper->add('userFirstname', TextType::class)
                       ->add('userImage', ModelListType::class);
        }
    

    Hope it helps!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题