xarion
xarion

Reputation: 470

Doctrine2 multiple relationships of two entities

We're using Doctrine v2.2.1. With YML defined entities.

Here i have 2 entities which are referring to each other with given associations;

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

When i generate the Entity classes according to these definitions, I get an unexpected result in my entities\User php class. Which is like;

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

The setter methods in my entities are generated well as expected. But the add methods for entities\User are not generated as expected.

Am I doing something wrong? Or is there any workaround to this? Or is it related with the issue referred in the Limitations and Known Issues doc of Doctrine2?

Peace

Upvotes: 1

Views: 433

Answers (1)

bdaylik
bdaylik

Reputation: 81

This is also one of the issues I came across using Doctrine ORM. Eventhough I don't know an elegant solution for this, I know that you can use get method to get the ORM collection and just add the entity you want. An example would be,

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

Or for the subjectNews

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

Hope this helps..

Upvotes: 2

Related Questions