Symfony Sonata Media Bundle 向用户添加图像/视频 [英] Symfony Sonata Media Bundle add images/videos to a user

查看:18
本文介绍了Symfony Sonata Media Bundle 向用户添加图像/视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Sonata Media Bundle 集成到我的项目中.问题是,我不明白捆绑包是如何工作的.

I am trying to integrate Sonata Media Bundle in my project. The problem is, that i don't understand how the bundle works.

它在应用程序"中生成了一个 Media、Gallery 和 GalleryHasMedia 类.它们是为了什么?我现在如何将图像字段和视频字段添加到我的用户实体?(均为复数)

It has generated a Media, Gallery and GalleryHasMedia class within 'Application'. What are they for? How can I now add an images field and a videos field to my User Entity ? (both plural)

问候,新星

推荐答案

媒体 是保存你的视频/图片的所有属性的实体:宽/高/文件路径...

Media is the Entity that saves all the properties of your video / picture : width / height / file path...

实体 Gallery 如果您想将多个媒体链接在一起很有用(关于同一主题的视频/图片库).

The Entity Gallery is useful if you want to link multiple Media together (gallery of videos / pictures about a same subject).

实体 GalleryHasMedia 是链接画廊和媒体的实体.

The Entity GalleryHasMedia is the Entity which links Gallery and Media.

SonataMedia 安装在捆绑应用程序中,因此您可以根据需要轻松扩展和更改代码.

SonataMedia is installed in a Bundle Application so you can extend and change easily the code to your needs.

如果您想向用户添加媒体或图库,您只需:

If you want to add a Media or a Gallery to a User you simply have to do :

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

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

使用控制台重新生成您的 getter 和 setter:

Regenerate your getter and setters with the console :

php app/console doctrine:generate:entities TestBundle:User

并且您已设置为在您的用户实体中使用 SonataMedia.

And you are set to use the SonataMedia in your User Entity.

更新

如果你想为一个用户管理多张图片,你必须这样做:

If you want to manage multiple images for a User, you have to do :

用户实体

class UserEntity
{
    /**
     * @var Media
     *
     * @ORM\OneToMany(targetEntity="Application\Sonata\MediaBundle\Entity\Media", mappedBy="user")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="images", referencedColumnName="id")
     * })
     */
    private $images;
}

应用程序\奏鸣曲\MediaBundle\实体\媒体

class Media
{
    /**
      * @var User
      *
      * @ORM\ManyToOne(targetEntity="UserEntity", inversedBy="images")
      * @ORM\JoinColumns({
      *     @ORM\JoinColumn(name="user", referencedColumnName="id")
      * })
      */
    private $user;
} 

用户管理员

class UserAdmin
{
    public function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('images', 'sonata_type_collection', array(), array(
            'edit' => 'inline',
            'inline' => 'table',
            'link_parameters' => array(
                'context' => 'images',
                'provider' => 'sonata.media.provider.image'
            )
        ))
    }
}

您可以通过更改编辑和内联属性来更改显示,link_parameters 设置表单的必需属性:上下文和提供程序

You could change the display by changing the edit and inline properties, link_parameters sets the mandatories properties for the form : context and provider

更新 2

问题 2

如果您想要一个用户拥有多个画廊,您只需执行我在之前更新中解释的相同过程,唯一的区别是您应该创建一个新属性,例如:private $imageGalleries with the targetEntity Gallery,添加在 Sonata 的 Gallery Entity 中使用 inversedBy 并在您的 SonataAdmin 类中添加新属性,只需将字段名称图像更改为 imageGalleries.

If you want multiple galleries for a user, you simply have to do the same process that I explained in my previous update, the only difference is you should create a new property for example : private $imageGalleries with the targetEntity Gallery, add the inversedBy in the Gallery Entity of Sonata and add in your SonataAdmin class the new property by only changing the fields name images to imageGalleries.

问题 3

在 Sonata 之外,您应该使用 Sonata_media_type 形式来处理媒体.http://sonata-project.org/bundles/media/2-0/doc/reference/form.html因为您有一个 oneToMany 关系,所以它将是一个 Sonata_media_type 的集合.

Outside of Sonata, you should use the sonata_media_type form to handle Media. http://sonata-project.org/bundles/media/2-0/doc/reference/form.html Because you have a oneToMany relations it will be a collection of sonata_media_type.

我知道没有处理画廊的表格.

There is no form to handle Galleries that I know.

这篇关于Symfony Sonata Media Bundle 向用户添加图像/视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆