0

我正在尝试处理一个对我来说非常复杂的表格......

我们有收藏,其中包含书籍 (OneToMany)、文章 (OneToMany) 及其作者 (ManyToMany)。

用户可以编辑一本书:他可以添加或删除一篇文章,并为每篇文章添加或删除一些作者。有嵌套形式:书籍>文章>作者。如果作者是集合中的新作者,则会为该集合创建它。

实体描述看起来不错,数据库由控制台生成并且看起来一致。

如果我不必与使用书籍版本表格的作者打交道,这很好用。如果作者存在,我有一个重复的条目错误。如果作者是新人,我有一个“显式持久化新实体或在关系上配置级联持久化操作”的错误。

这是代码:

public function onSuccess(Book $book)
{   
    $this->em->persist($book);

    foreach($book->getArticles() as $article)  
    {
        $article->setUrlname($this->mu->generateUrlname($article->getName()));
        $article->setBook($book);

        // Saving (and creating) the authors of the book
        foreach ($this->collectionWithAuthors->getAuthors() as $existAuthor){      
            foreach($article->getAuthors() as $author) {                        
                $authorUrlname=$this->mu->generateUrlname($author->getFirstname().' '.$author->getLastname());
                if ( $existAuthor->getUrlname() ==  $authorUrlname) { // The author is existing
                    $article->addAuthor($existAuthor);
                    $this->em->persist($existAuthor);
                }else{                                                // New Author
                    $newAuthor = new Author();                                
                    $newAuthor->setCollection($this->collectionWithBaseArticles);
                    $newAuthor->setLastname($author->getLastname());
                    $newAuthor->setFirstname($author->getFirstname());
                    $newAuthor->setUrlname($authorUrlname);
                    $this->em->persist($newAuthor);
                    $article->addAuthor($newAuthor);                          
                }
            }
        }
        $this->em->persist($article);

    }
    $this->em->flush();

}

我不知道如何使用级联。但是 $article->addAuthor() 应该调用 $authors->addArticle():

文章实体摘录

/**
* @ORM\ManyToMany(targetEntity="bnd\myBundle\Entity\Author", mappedBy="articles")
*/
private $authors;

/**
 * Add authors
 *
 * @param bnd\myBundle\Entity\Author $authors
 * @return Article
 */

public function addAuthor(\bnd\myBundle\Entity\Author $authors)
{
    $this->authors[] = $authors;
    $authors->addArticle($this);
}
4

1 回答 1

2

foreach 语句中的逻辑是错误的。假设我们有下一位作者:

  1. 持续作者(collectionWithAuthors):
    • 约翰
    • 埃里克
  2. 提交的作者
    • 艾达
    • 埃里克

因此,对于每个现有作者(John 和 Eric),脚本会通过新作者循环:

foreach ([John, Eric] as $author) {
    foreach([Ada, Eric] as $newAuthor) {
        // John author: the script persist Ada(right) and Eric(wrong) as new authors
        // Eric author: the script persist Ada(wrong), but not Eric(right)
    }
}

解决办法是用现有作者替换文章作者(如果有类似的话)

foreach ($article->getAuthors() as $key => $articleAuthor) {
    $authorUrlname=$this->mu->generateUrlname($articleAuthor->getFirstname().' '.$articleAuthor->getLastname());
    $foundAuthor = false;
    // Compare article author with each existing author
    foreach ($this->collectionWithAuthors->getAuthors() as $existAuthor) { 
        if ($existAuthor->getUrlname() ==  $authorUrlname) {
            $foundAuthor = true;
            break; // It has found similar author no need to look further
        }
    }

    // Use $existAuthor as found one, otherwise use $articleAuthor
    if ($foundAuthor) {
        $article->removeAuthor($articleAuthor); // Remove submitted author, so he wont be persisted to database
        $article->addAuthor($existAuthor);
    } else {
        // Here you dont need to create new author                          
        $articleAuthor->setCollection($this->collectionWithBaseArticles);
        $articleAuthor->setUrlname($authorUrlname);
    }

    ...
}

$this->_em->persist($article);

您已经注意到我从循环中删除了任何持久的作者,为了持久保存这些作者,最好在文章实体类的 $authors 注释中设置 cascade={'persist'}

/**
 * @ORM\ManyToMany(targetEntity="Author", cascade={"persist"})
 * @ORM\JoinTable(...)
 */
protected $authors;

升级版:

我忘了提到关于级联持久性的一件事。要保持文章和作者之间的关系,您还必须添加与作者实体的关系。编辑实体中的addAuthor()方法Article如下:

public function addAuthor(Author $author)
{
    // Only add author relation if the article does not have it already
    if (!$this->authors->contains($author)) {
        $this->authors[] = $author;
        $author->addArticle($this);
    }
}

此外,最好在构造函数中为实体集合定义默认值:

use Doctrine\Common\Collections\ArrayCollection;

// ...

public function __construct()
{
    $this->authors = new ArrayCollection();
}
于 2012-10-25T10:19:52.863 回答