学说2 两个实体的多重关系


Doctrine2 multiple relationships of two entities

我们正在使用Doctrine v2.2.1。使用 YML 定义的实体。

在这里,我有 2 个实体,它们以给定的关联相互引用;

entities'User:
  type: entity
  table: user
  oneToMany:
    subjectNews:
      targetEntity: entities'News
      mappedBy: subjectUser
      cascade: ["all"]
    actionNews:
      targetEntity: entities'News
      mappedBy: actionUser
      cascade: ["all"]
entities'News:
  type: entity
  table: news
  manyToOne:
    subjectUser:
      targetEntity: entities'User
      cascade: ["all"]
      nullable: true
    actionUser:
      targetEntity: entities'User
      cascade: ["all"]
      nullable: true

当我根据这些定义生成实体类时,我的实体''用户 php 类中出现意外结果。这就像;

 /**
  * Add subjectNews
  *
  * @param entities'News $subjectNews
  * @return User
  */
 public function addNews('entities'News $subjectNews)
 {
     $this->subjectNews[] = $subjectNews;
     return $this;
 }

我的实体中的 setter 方法按预期生成良好。但是实体''用户的添加方法不会按预期生成。

我做错了什么吗?或者有什么解决方法吗?或者它是否与 Doctrine2 的限制和已知问题文档中提到的问题有关?

和平

这也是我在使用Doctrine ORM时遇到的问题之一。尽管我不知道对此的优雅解决方案,但我知道您可以使用 get 方法来获取 ORM 集合并添加您想要的实体。一个例子是,

$actionNews = $user->getActionNews();
$actionNews[] = new entities'News();

或者为了subjectNews

$subjectNews = $user->getSubjectNews();
$subjectNews[] = new entities'News();

希望这有帮助..