src/Sulu/Bundle/ContactBundle/Controller/AbstractMediaController.php
protected function addMediaToEntity($entityName, $id, $mediaId)
    {
        try {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository($entityName)->find($id);
            $media = $this->container->get('sulu.repository.media')->find($mediaId);

            if (!$entity) {
                throw new EntityNotFoundException($entityName, $id);
            }

            if (!$media) {
                throw new EntityNotFoundException($this->getParameter('sulu.model.media.class'), $mediaId);
            }

            if ($entity->getMedias()->contains($media)) {
                throw new RestException('Relation already exists');
            }

            $entity->addMedia($media);
            $em->flush();

            $view = $this->view(
                new Media(
                    $media,
                    $this->getUser()->getLocale(),
                    null
                ),
                200
            );
        } catch (EntityNotFoundException $enfe) {
            $view = $this->view($enfe->toArray(), 404);
        } catch (RestException $exc) {
            $view = $this->view($exc->toArray(), 400);
        } catch (\Exception $e) {
            $view = $this->view($e->getMessage(), 400);
        }

        return $this->handleView($view);
    }
protected function addMediaToEntity($entityName, $id, $mediaId)
    {
        try {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository($entityName)->find($id);
            $media = $this->container->get('sulu.repository.media')->find($mediaId);

            if (!$entity) {
                throw new EntityNotFoundException($entityName, $id);
            }

            if (!$media) {
                throw new EntityNotFoundException($this->getParameter('sulu.model.media.class'), $mediaId);
            }

            if ($entity->getMedias()->contains($media)) {
                throw new RestException('Relation already exists');
            }

            $entity->addMedia($media);
            $em->flush();

            $view = $this->view(
                new Media(
                    $media,
                    $this->getUser()->getLocale(),
                    null
                ),
                200
            );
        } catch (EntityNotFoundException $enfe) {
            $view = $this->view($enfe->toArray(), 404);
        } catch (RestException $exc) {
            $view = $this->view($exc->toArray(), 400);
        } catch (\Exception $e) {
            $view = $this->view($e->getMessage(), 400);
        }

        return $this->handleView($view);
    }
protected function addMediaToEntity ( $ entityName , $ id , $ mediaId ) { try { $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ entity = $ em -> getRepository ( $ entityName ) -> find ( $ id ) ; $ media = $ this -> container -> get ( 'sulu.repository.media' ) -> find ( $ mediaId ) ; if ( ! $ entity ) { throw new EntityNotFoundException ( $ entityName , $ id ) ; } if ( ! $ media ) { throw new EntityNotFoundException ( $ this -> getParameter ( 'sulu.model.media.class' ) , $ mediaId ) ; } if ( $ entity -> getMedias ( ) -> contains ( $ media ) ) { throw new RestException ( 'Relation already exists' ) ; } $ entity -> addMedia ( $ media ) ; $ em -> flush ( ) ; $ view = $ this -> view ( new Media ( $ media , $ this -> getUser ( ) -> getLocale ( ) , null ) , 200 ) ; } catch ( EntityNotFoundException $ enfe ) { $ view = $ this -> view ( $ enfe -> toArray ( ) , 404 ) ; } catch ( RestException $ exc ) { $ view = $ this -> view ( $ exc -> toArray ( ) , 400 ) ; } catch ( \ Exception $ e ) { $ view = $ this -> view ( $ e -> getMessage ( ) , 400 ) ; } return $ this -> handleView ( $ view ) ; }
Adds a relation between a media and the entity.

@param string $entityName
@param string $id
@param string $mediaId

@return Media
Adds a relation between a media and the entity .