2011-10-19 92 views
1

我想使3個實體(項目,同意,不同意)與以下關係。Doctrine2不能使'2'一對多關係

  • 項目一一對多同意
  • 項目一到多的不同意

但只有一個關係(後聲明)了兩取得。

這裏是我的.yml文件。


Entities\Item: 
    type: entity 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    oneToMany: 
    agrees: 
     targetEntity: Agree 
     mappedBy: items 
    oneToMany: 
    disagrees: 
     targetEntity: Disagree 
     mappedBy: items 

Entities\Agree: 
    type: entity 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    manyToOne: 
    items: 
     targetEntity: Item 
     inversedBy: agrees 

Entities\Disagree: 
    type: entity 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    manyToOne: 
    items: 
     targetEntity: Item 
     inversedBy: disagrees 

和下面的代碼是通過在Doctrine2生成自動Item.php。正如你所看到的,它根本不包含「同意」。


namespace Entities; 

class Item { 
    private $id; 
    private $disagrees; 

    public function __construct() { 
     $this->disagrees = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function addDisagrees(\Entities\Disagree $disagrees) { 
     $this->disagrees[] = $disagrees; 
    } 

    public function getDisagrees() { 
     return $this->disagrees; 
    } 
} 

如果我切換聲明順序( '不同意' 第一,其次by'Agree '像下面),Item.php僅具有' 在此時間Agree'相關的代碼。


Entities\Item: 
    type: entity 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    oneToMany: 
    disagrees: 
     targetEntity: Disagree 
     mappedBy: items 
    oneToMany: 
    agrees: 
     targetEntity: Agree 
     mappedBy: items 

這有什麼錯我的代碼?任何意見都會有幫助。

項目,同意和不同意只是示例來顯示此問題。在實際項目中,同意和反對是完全不同的實體。所以,不要建議我把它們合併成統一的實體。 :)

回答

8

你被關閉,你只需要把所有相同的關聯映射類型相同的類型聲明:)

oneToMany: 
    agrees: 
     targetEntity: Agree 
     mappedBy: items 
    disagrees: 
     targetEntity: Disagree 
     mappedBy: items