/** * Add author with type to article * * @param Article $article * @param Author $author * @param ArticleAuthor $authorType * * @return ArticleAuthor */ public function addAuthorToArticle(Article $article, Author $author, AuthorType $authorType) { $articleAuthor = $this->em->getRepository('Newscoop\\Entity\\ArticleAuthor')->getArticleAuthor($article->getNumber(), $article->getLanguageCode(), $author->getId(), $authorType->getId())->getOneOrNullResult(); if (!is_null($articleAuthor)) { throw new ResourcesConflictException("Author with this type is already attached to article", 409); } $articleAuthors = $this->em->getRepository('Newscoop\\Entity\\ArticleAuthor')->getArticleAuthors($article->getNumber(), $article->getLanguageCode())->getArrayResult(); $articleAuthor = new ArticleAuthor(); $articleAuthor->setArticle($article); $articleAuthor->setAuthor($author); $articleAuthor->setType($authorType); $articleAuthor->setOrder(count($articleAuthors) + 1); $this->em->persist($articleAuthor); $this->em->flush(); return $articleAuthor; }