src/Controller/Api/GalleryController.php
public function postGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Request $request)
    {
        $gallery = $this->getGallery($galleryId);
        $media = $this->getMedia($mediaId);

        foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
            if ($galleryHasMedia->getMedia()->getId() === $media->getId()) {
                return FOSRestView::create([
                    'error' => sprintf('Gallery "%s" already has media "%s"', $galleryId, $mediaId),
                ], 400);
            }
        }

        return $this->handleWriteGalleryhasmedia($gallery, $media, null, $request);
    }
public function postGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Request $request)
    {
        $gallery = $this->getGallery($galleryId);
        $media = $this->getMedia($mediaId);

        foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
            if ($galleryHasMedia->getMedia()->getId() === $media->getId()) {
                return FOSRestView::create([
                    'error' => sprintf('Gallery "%s" already has media "%s"', $galleryId, $mediaId),
                ], 400);
            }
        }

        return $this->handleWriteGalleryhasmedia($gallery, $media, null, $request);
    }
public function postGalleryMediaGalleryhasmediaAction ( $ galleryId , $ mediaId , Request $ request ) { $ gallery = $ this -> getGallery ( $ galleryId ) ; $ media = $ this -> getMedia ( $ mediaId ) ; foreach ( $ gallery -> getGalleryHasMedias ( ) as $ galleryHasMedia ) { if ( $ galleryHasMedia -> getMedia ( ) -> getId ( ) === $ media -> getId ( ) ) { return FOSRestView :: create ( [ 'error' => sprintf ( 'Gallery "%s" already has media "%s"' , $ galleryId , $ mediaId ) , ] , 400 ) ; } } return $ this -> handleWriteGalleryhasmedia ( $ gallery , $ media , null , $ request ) ; }
Adds a media to a gallery.

@ApiDoc(
requirements={
{"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"},
{"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"}
},
input={"class"="sonata_media_api_form_gallery_has_media", "name"="", "groups"={"sonata_api_write"}},
output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}},
statusCodes={
200="Returned when successful",
400="Returned when an error has occurred while gallery/media attachment",
}
)

@param int     $galleryId A gallery identifier
@param int     $mediaId   A media identifier
@param Request $request   A Symfony request

@throws NotFoundHttpException

@return GalleryInterface
Adds a media to a gallery .