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
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.