2016-03-03 103 views
4

多重繼承我得到了下面的UML方案: enter image description hereSymfony2的抽象類學說

基本上,它是分類系統的其中一些嵌套的,有的不是開始。 我開始嘗試製作2層抽象類(Taxonomy和OfferCategory),因爲它們都不能用作最終實體。我用MappedSuperClass,但我得到了以下錯誤:

[Doctrine\ORM\ORMException]                      
Column name `id` referenced for relation from LCH\CatalogBundle\Entity\HomeOfferCategory towards LCH\CatalogBund  le\Entity\OfferCategory does not exist. 

我的主鍵字段是ID ...

在從更普遍的觀點,什麼是對我的方案提供主義的最好的實現?

謝謝!

編輯:我試着直接在我的RootOfferCategory類中轉置所有OfferCategory成員。通過改變雙方的目標定位,不會有更多的錯誤。 含義你不能自引用映射的超類?

分類:

/** 
* Class Taxonomy 
* @package LCH\CatalogBundle\Entity 
* @ORM\MappedSuperclass 
*/ 
abstract class Taxonomy implements TaxonomyInterface 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var string the category name 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    protected $name; 
} 

** OfferCategory:**

/** 
* OfferCategory 
* @ORM\MappedSuperclass 
*/ 
abstract class OfferCategory extends Taxonomy 
{ 
    /** 
    * @var OfferCategory the category parent 
    * @ORM\ManyToOne(targetEntity="LCH\CatalogBundle\Entity\OfferCategory",inversedBy="children", cascade={"persist"}) 
    * @ORM\JoinColumn(name="parent_id", referenceColumnName="id") 
    */ 
    protected $parent; 
    /** 
    * @var OfferCategory the children categories 
    *  @ORM\OneToMany(targetEntity="LCH\CatalogBundle\Entity\OfferCategory",mappedBy="parent", cascade={"persist"}) 
    */ 
    protected $children; 
} 

RootOfferCategory

/** 
* RootOfferCategory 
* Represents one root top category 
* @ORM\Table() 
*  @ORM\Entity(repositoryClass="LCH\CatalogBundle\Entity\RootOfferCategoryRepository") 
*/ 
class RootOfferCategory extends OfferCategory 
{ 

} 

回答

0

對不起戰爭之後抵達。

從學說文檔this part

映射超不能是一個實體,它不是可查詢和由映射超定義的持久關係必須是單向的(只有一個持有端)。

這意味着一對多關聯在映射超類上根本不可能。
此外,多對多關聯只有在映射超類只用於一個實體的情況下才有可能。目前。

爲了進一步支持繼承,必須使用單表或繼承表繼承功能。

+0

感謝您的回答,我發現雖然再次閱讀Doctrine2文檔......但是,有沒有另一種方法來模擬我的情況,以保持父母關係的獨特性和不重複? – nbonniot

+1

擴展一個實體(實際上不需要'@ORM \ Entity'註釋)而不是映射的超類,子實體將繼承其映射的屬性。我使用了一個簡單的類,它只包含一個映射的標識符,以避免將它寫入所有實體中,並且它完美地工作,無論是否帶有實體註釋,抽象與否......規則與類繼承一起工作良好。 – chalasr