考虑以下问题:
我在 Article
和 Author
之间有一个多对一 关联。
class Author{
/**
* ...
*@var ArrayCollection_of_Article
*/
public $articles;
}
class Article{
/**
* ...
*@var Author
*/
public $author;
}
制作一个新的文章
我有两种类型的代码:
第一个:
$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错误。
谢谢...
最佳答案
您必须使用 getter 和 setter 访问关系,这是因为 Doctrine 构建了一个从您的 Entity 类扩展的 Proxy 类,并覆盖了您的 getter 以提供延迟加载。
关于php - 学说关联映射和 arrayCollection 持续错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231937/