2012-04-17 14 views
1

私たちはDoctrine v2.2.1を使用しています。 YMLによって定義されたエンティティ。Doctrine2 2つのエンティティの複数の関係

ここでは、私は与えられた関連付けをお互いに参照している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; 
} 

私のエンティティのセッターメソッドは期待どおりに生成されます。ただし、エンティティ\ユーザーの追加メソッドは期待どおりに生成されません。

何か間違っていますか?または、これに対処する方法はありますか?それとも、the issue referred in the Limitations and Known Issues doc of Doctrine2と関連していますか?

平和

+0

チェックアウト[この回答] [1] [1]のために、だろう://stackoverflow.com/questions/6299738/doctrine-symfony-multiple-one-to-many-relations-on-same-model – frail

答えて

2

また、これは私が教義ORMを使用して出会った問題の一つです。このためのエレガントなソリューションはわかりませんが、getメソッドを使用してORMコレクションを取得し、必要なエンティティを追加することができます。 http:例は

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

それともsubjectNews

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

・ホープ、このことができます。..

+0

Works ..しかし、その汚れ..その解決策は時にはトラブルを引き起こす。例えば; EntityManagerがダーティコンテキストを検出できないことがあります。私はDoctrineエンティティが実際にどのように動作するのか分からないので、Userエンティティから定義を削除することに固執します。 – xarion

関連する問題