doctrine association mapping和arrayCollection持久化错误

时间:2012-09-01 22:30:02

标签: php mysql database doctrine-orm

考虑以下问题:
我在ArticleAuthor之间有多对一关联。

class Author{
  /**
   * ...
   *@var ArrayCollection_of_Article
   */
   public $articles;
}

class Article{
   /**
   * ...
   *@var Author
   */
   public $author;
}

制作新Article我有两种类型的代码:
第一名:

$author = ORM::Find("Author",12); // fetch an Author with ID=12
$art = new Article();
$art->author=$author;
$author->articles->add($art);
ORM::Persist($art); // persist it to write to database

第二个:(省略第4行)

$author = ORM::Find("Author",12); // fetch an Author with ID=12
$art = new Article();
$art->author=$author;
ORM::Persist($art); // persist it to write to database

哪一个是正确的?
第一个正常工作。但第二个有时会导致错误,如下面的错误:

A new entity was found through a relationship that was not configured to cascade persist operations  

我想知道第二个是否可行,或者总是导致sql错误。
谢谢......

1 个答案:

答案 0 :(得分:0)

您必须使用getter和setter访问关系,这是因为Doctrine构建了一个从您的Entity类扩展的Proxy类,并覆盖您的getter以提供延迟加载。