加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony2抽象类与Doctrine的多重继承

发布时间:2020-12-13 22:25:47 所属栏目:PHP教程 来源:网络整理
导读:我得到了以下UML方案: 基本上,它是分类系统的开始,其中一些是可嵌套的,有些则不是. 我开始尝试制作2层抽象类(Taxonomy和OfferCategory),因为它们都不能用作最终实体.我使用了MappedSuperClass,但是我收到了以下错误: [DoctrineORMORMException] Column n
我得到了以下UML方案:

enter image description here

基本上,它是分类系统的开始,其中一些是可嵌套的,有些则不是.
我开始尝试制作2层抽象类(Taxonomy和OfferCategory),因为它们都不能用作最终实体.我使用了MappedSuperClass,但是我收到了以下错误:

[DoctrineORMORMException]                                                                                       
Column name `id` referenced for relation from LCHCatalogBundleEntityHomeOfferCategory towards LCHCatalogBund       leEntityOfferCategory does not exist.

我的主要关键字段是id …

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

谢谢 !

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

分类 :

/**
* Class Taxonomy
* @package LCHCatalogBundleEntity
* @ORMMappedSuperclass
*/
abstract class Taxonomy implements TaxonomyInterface
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id",type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

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

** OfferCategory:**

/**
 * OfferCategory
 * @ORMMappedSuperclass
 */
abstract class OfferCategory extends Taxonomy
{
   /**
    * @var OfferCategory the category parent
    * @ORMManyToOne(targetEntity="LCHCatalogBundleEntityOfferCategory",inversedBy="children",cascade={"persist"})
    * @ORMJoinColumn(name="parent_id",referenceColumnName="id")
    */
    protected $parent;
   /**
   * @var OfferCategory the children categories
   *       @ORMOneToMany(targetEntity="LCHCatalogBundleEntityOfferCategory",mappedBy="parent",cascade={"persist"})
   */
   protected $children;
}

RootOfferCategory

/**
 * RootOfferCategory
 * Represents one root top category
 * @ORMTable()
 *      @ORMEntity(repositoryClass="LCHCatalogBundleEntityRootOfferCategoryRepository")
 */
class RootOfferCategory extends OfferCategory
{

}

解决方法

对不起,战后到了.

从this part的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读