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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择