Symfony Sonata Media Bundle add images/videos to a user

11,080

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

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

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

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;
}

Regenerate your getter and setters with the console :

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

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

UPDATE

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

UserEntity

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

Application\Sonata\MediaBundle\Entity\Media

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

UserAdmin

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'
            )
        ))
    }
}

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

UPDATE 2

Question 2

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.

Question 3

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.

Share:
11,080
nova.cp
Author by

nova.cp

Updated on June 06, 2022

Comments

  • nova.cp
    nova.cp almost 2 years

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

    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)

    Regards, nova

  • nova.cp
    nova.cp about 9 years
    I think i need to clarify myself. I want to add videos and images to my user entity. For that, i need some sample code. I know how to add a OneToOne relationship between User and Media, but do I need now to add a new "Image" and "Video" class in the Application/Sonata/Entity namespace and map these classes to fields in my user entity (i don't want one big media class for all medias, i want to seperate images, videos and files) ? How do i need to build now the form type ? Please share some sample code with me :) In addition, i do not understand the scence of the "Gallery" class ..?
  • nova.cp
    nova.cp about 9 years
    I don't understand your mappings. At first you say "Many User have one image", but what I wanted was "One User has many images". And for the gallery, the mappings of your code would mean "One gallery to many users", but i think "Many galleries to one user" would make more sense. I understand when i do ManyToOne relation, because there i only need the annotation in one class. But what, when i have OneToMany ? There, I need annotations in both classes ..? Can you update your answer for a OneToMany relation please :)
  • nova.cp
    nova.cp about 9 years
    I mean one user should have more galleries, because a user should have a gallery of images, a gallery of videos and e.g. a gallery of documents. Or do i create therefore different fields that i map to the media entity? How is that done ?
  • HypeR
    HypeR about 9 years
    You should create 3 different properties using the Entity Gallery or Media with the OnetoMany type. Choosing between Gallery and Media depends on your project if Medias are independent or not. Good luck !
  • nova.cp
    nova.cp about 9 years
    Ok, thank you i begin to understand. I have three questions left before i can accept this answer :) 1) I don't understand yet how to add now e.g. a 'videos' property to my user, because in the user entity, i would add the same as i did with 'images', but how does my "Media" class than look like ? The 'inversedBy' on the user property is already taken for images. 2) The difference between Media and Gallery. How can i add images to a gallery, so that they are 'dependent'? 3)In a form type outside the admin area i would use a normal 'collection' field with type = 'sonata.media.provider.image'?:)
  • nova.cp
    nova.cp about 9 years
  • HypeR
    HypeR about 9 years
    I answered your question 2 & 3.