考虑以下问题:
我在和之间存在多对一关联。 Article
Author
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 错误。
谢谢...