选择性地继承实体的部分 [英] Selectively inherit parts of an entity

查看:142
本文介绍了选择性地继承实体的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从一个父实体扩展的实体。



我想从其中一个删除一个或多个列,同时保持继承。

我试图通过映射找到一个解决方案父实体为 MappedSuperClass ,但没有帮助。



示例:

 <?php 

/ ** @ ORM\Entity * /
class Base
{
/ ** @ORMColumn(name =foo,type =string)* /
protected $ foo;

/ ** @ORMColumn(name =bar,type =string)* /
protected $ bar

}

/ **
* @ ORM\Entity
* /
类子扩展Base
{
//只需要Base :: $ bar列映射
//而不是Base :: $ foo列映射
}

整个继承地图



有没有办法实现这一点?

解决方案

您不能选择性地继承实体类的部分。听起来你需要重构Base类,或者根据你的其他类在其他类之间共享的属性引入另一个抽象类。



ie

  / ** 
* @MappedSuperclass
* /
class Base
{
/ ** @ORMColumn(name =foo,type =string)* /
private $ foo;
}

/ **
* @MappedSuperclass
* /
class SomeOtherBase extends Base
{
/ ** @ ORMColumn(name =bar,type =string)* /
private $ bar
}


/ **
* @ORM \Entity
* /
class Child extends Base
{
//只需要Base :: $ bar列映射
//而不是Base: :$ foo列映射
}


I have many entities extending from one parent entity.

I want remove one or more column from only one of them, while keeping the inheritance.
I've tried to find a solution by mapping the parent entity as MappedSuperClass but it doesn't help.

Example :

<?php

/** @ORM\Entity */
class Base
{
    /** @ORMColumn(name="foo", type="string") */
    protected $foo;

    /** @ORMColumn(name="bar", type="string") */
    protected $bar

}

/**
 * @ORM\Entity
 */
class Child extends Base
{
    // How take only the Base::$bar column mapping 
    // and not the Base::$foo column mapping
}

The entire Inheritance mapping chapter of the doctrine documentation doesn't give me any alternative.

I need to really remove/exclude the columns from database, serialisation doesn't solve my problem.

Is there a way to achieve this ?

解决方案

You can't selectively inherit parts of an entity class. It sounds like you need to refactor your Base class or perhaps introduce another abstract class depending on which properties your other classes share among each other.

i.e.

/**
 * @MappedSuperclass
 */
class Base
{
    /** @ORMColumn(name="foo", type="string") */
    private $foo;
}

/**
 * @MappedSuperclass
 */
class SomeOtherBase extends Base
{
    /** @ORMColumn(name="bar", type="string") */
    private $bar
}


/**
* @ORM\Entity
*/
class Child extends Base
{
    // How take only the Base::$bar column mapping 
    // and not the Base::$foo column mapping
}    

这篇关于选择性地继承实体的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆