Sulu\Bundle\ContactBundle\Controller\AbstractMediaController::addMediaToEntity PHP Метод

addMediaToEntity() защищенный Метод

Adds a relation between a media and the entity.
protected addMediaToEntity ( string $entityName, string $id, string $mediaId ) : Media
$entityName string
$id string
$mediaId string
Результат Sulu\Bundle\MediaBundle\Api\Media
    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);
    }