src/Sulu/Bundle/ContactBundle/Controller/AbstractMediaController.php
protected function removeMediaFromEntity($entityName, $id, $mediaId)
    {
        try {
            $delete = function() use ($entityName, $id, $mediaId) {
                $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);
                }

                $mediaEntityName = $this->getParameter('sulu.model.media.class');

                if (!$media) {
                    throw new EntityNotFoundException($mediaEntityName, $mediaId);
                }

                if (!$entity->getMedias()->contains($media)) {
                    throw new RestException(
                        'Relation between ' . $entityName .
                        ' and ' . $mediaEntityName . ' with id ' . $mediaId . ' does not exists!'
                    );
                }

                $entity->removeMedia($media);
                $em->flush();
            };

            $view = $this->responseDelete($id, $delete);
        } 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 removeMediaFromEntity($entityName, $id, $mediaId)
    {
        try {
            $delete = function() use ($entityName, $id, $mediaId) {
                $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);
                }

                $mediaEntityName = $this->getParameter('sulu.model.media.class');

                if (!$media) {
                    throw new EntityNotFoundException($mediaEntityName, $mediaId);
                }

                if (!$entity->getMedias()->contains($media)) {
                    throw new RestException(
                        'Relation between ' . $entityName .
                        ' and ' . $mediaEntityName . ' with id ' . $mediaId . ' does not exists!'
                    );
                }

                $entity->removeMedia($media);
                $em->flush();
            };

            $view = $this->responseDelete($id, $delete);
        } 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 removeMediaFromEntity ( $ entityName , $ id , $ mediaId ) { try { $ delete = function ( ) use ( $ entityName , $ id , $ mediaId ) { $ 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 ) ; } $ mediaEntityName = $ this -> getParameter ( 'sulu.model.media.class' ) ; if ( ! $ media ) { throw new EntityNotFoundException ( $ mediaEntityName , $ mediaId ) ; } if ( ! $ entity -> getMedias ( ) -> contains ( $ media ) ) { throw new RestException ( 'Relation between ' . $ entityName . ' and ' . $ mediaEntityName . ' with id ' . $ mediaId . ' does not exists!' ) ; } $ entity -> removeMedia ( $ media ) ; $ em -> flush ( ) ; } ; $ view = $ this -> responseDelete ( $ id , $ delete ) ; } 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 ) ; }
Removes a media from the relation with an entity.

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

@return Response
Removes a media from the relation with an entity .