php - 学说关联映射和 arrayCollection 持续错误

标签 php mysql database doctrine-orm

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

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/

相关文章:

MySql 查询连接速度慢 - 如何加快速度

mysql - 选择列值较上一行发生变化的行、用户变量、innodb

php - 如何让 php 在 E_NOTICE 上退出?

php - PHP 网站安全问题? (可能也适用于 ASP/Rails/等)

php - preg_match : number-alphabets and commas only

sql-server - 表 Visual Basic 数据库 (T-SQL) 中的条件 If 语句

php - 在 Php $db->select 上选择数据库

c# - 从 C# Windows 窗体调用 PHP Web 服务

MySQL C API 自定义值

php - 复杂的嵌套数组问题