7

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

它在“应用程序”中生成了一个 Media、Gallery 和 GalleryHasMedia 类。它们是干什么用的?我现在如何向我的用户实体添加图像字段和视频字段?(都是复数)

问候,新星

4

2 回答 2

22

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

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

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

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

如果您想向用户添加媒体或图库,您只需执行以下操作:

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:

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

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

更新

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

用户实体

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 设置表单的强制属性:上下文和提供程序

更新 2

问题2

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

问题 3

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

据我所知,没有任何形式可以处理画廊。

于 2015-03-15T21:07:52.207 回答
0

From the documentation:

"Note

The command will generate domain objects in an Application namespace. So you can point entities’ associations to a global and common namespace. This will make Entities sharing very easier as your models will allow to point to a global namespace. For instance the media will be Application\Sonata\MediaBundle\Entity\Media." http://sonata-project.org/bundles/media/2-2/doc/reference/installation.html

Pretty much what you get whenever you use the easy-extands bundle.

You use them just like any entity, just from a different namespace to your exisitng Entities.

As to using them in another form type, just embed the media form type in your user form type: http://sonata-project.org/bundles/media/2-2/doc/reference/form.html

To add the fields to another entity you just add the property with getters and setters and (we always use doctrine annotations not yaml) add the annotation for the media entity as the target entity with the column name for the type of relationship (1:1,1:M,M:M, etc) in the usual symfony way.

于 2015-03-15T20:18:59.677 回答