Symfony2抽象类与Doctrine的多重继承

时间:2021-11-26 06:45:46

I got following UML scheme : Symfony2抽象类与Doctrine的多重继承

我遵循UML方案:

Basically, it's the beginning of taxonomy system with some of them nestable, and some not. I started to try making 2 layers of abstract classes (Taxonomy and OfferCategory) because none of them can be used as a final entity. I used MappedSuperClass, but I got following error :

基本上,它是分类系统的开始,其中一些是可嵌套的,有些则不是。我开始尝试制作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.

My primary key field is id...

我的主要关键字段是id ...

In a more general point of view, what's the best implementation for my scheme provided with Doctrine?

从更一般的角度来看,Doctrine提供的计划的最佳实现是什么?

Thanks !

EDIT : I tried to transpose all OfferCategory members directly in my RootOfferCategory class. By changing th targetENtity on both sides, no more error. Meaning you can't self reference a mapped super class?

编辑:我试图直接在我的RootOfferCategory类中转置所有OfferCategory成员。通过改变双方的targetENtity,不再有错误。意思是你不能自己引用映射的超类?

Taxonomy :

/**
* 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:**

/**
 * 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
{

}

1 个解决方案

#1


0  

Sorry to arrive after the war.

对不起,战争结束后到达。

From this part of the Doctrine documentation :

从Doctrine文档的这一部分:

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only).

映射的超类不能是实体,它不是可查询的,并且由映射的超类定义的持久关系必须是单向的(仅具有拥有方)。

This means that One-To-Many associations are not possible on a mapped superclass at all.
Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity.At the moment.

这意味着根本不可能在映射的超类上进行一对多关联。此外,只有当映射的超类仅在一个实体中使用时,才能实现多对多关联。目前。

For further support of inheritance, the single or joined table inheritance features have to be used.

为了进一步支持继承,必须使用单个或连接表继承功能。

#1


0  

Sorry to arrive after the war.

对不起,战争结束后到达。

From this part of the Doctrine documentation :

从Doctrine文档的这一部分:

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only).

映射的超类不能是实体,它不是可查询的,并且由映射的超类定义的持久关系必须是单向的(仅具有拥有方)。

This means that One-To-Many associations are not possible on a mapped superclass at all.
Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity.At the moment.

这意味着根本不可能在映射的超类上进行一对多关联。此外,只有当映射的超类仅在一个实体中使用时,才能实现多对多关联。目前。

For further support of inheritance, the single or joined table inheritance features have to be used.

为了进一步支持继承,必须使用单个或连接表继承功能。