2012-04-17 5 views
1

Doctrine v2.2.1을 사용하고 있습니다. YML으로 정의 된 엔티티.Doctrine2 두 엔티티의 다중 관계

여기에는 주어진 연결을 통해 서로를 참조하는 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 메서드는 예상대로 잘 생성됩니다. 그러나 entities \ User의 추가 메소드는 예상대로 생성되지 않습니다.

내가 잘못 했나요? 아니면 이에 대한 해결 방법이 있습니까? 아니면 the issue referred in the Limitations and Known Issues doc of Doctrine2과 관련이 있습니까?

평화

+0

체크 아웃 [이 답변]가 될 것이다 [1] [1] : HTTP : //stackoverflow.com/questions/6299738/doctrine-symfony-multiple-one-to-many-relations-on-same-model – frail

답변

2

이것은 또한 내가 교리 ORM을 사용하여 가로 질러 온 문제 중 하나입니다. Eventhough 나는 이것에 대한 우아한 해결책을 모르지만, 나는 당신이 ORM 콜렉션을 얻고 단지 원하는 엔티티를 추가하기 위해 get 메소드를 사용할 수 있다는 것을 알고 있습니다. 예는

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

또는이 도움이 subjectNews

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

희망에 대한

..

+0

작동 ..하지만 더러운 .. 그 해결책은 때로는 문제를 일으킬 수 있습니다. 예를 들어; EntityManager가 더티 컨텍스트를 감지하지 못할 수 있습니다. Doctrine 엔티티가 실제로 어떻게 작동하는지 모르겠으므로 사용자 엔티티의 정의를 제거하는 데 전념 할 것입니다. – xarion