问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/28921077/symfony-sonata-media-bundle-add-images-videos-to-a-user