有选择地继承实体的各个部分


Selectively inherit parts of an entity

>我有许多实体从一个父实体扩展而来。

我想只从其中一个中删除一列或多列,同时保留继承。
我试图通过将父实体映射为 MappedSuperClass 来找到解决方案,但它没有帮助。

例:

<?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
}

教义文档的整个继承映射章节都没有给我任何选择。

我需要真正从数据库中删除/排除列,序列化并不能解决我的问题。

有没有办法做到这一点?

不能

有选择地继承实体类的某些部分。听起来你需要重构你的基类,或者引入另一个抽象类,这取决于你的其他类在彼此之间共享的属性。

/**
 * @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
}    

PHP 不允许通过继承来删除类的功能,它只是为了处理相反的事情而制定并被认为。

见 http://php.net/manual/en/language.oop5.inheritance.php

您可以使用特征和重构继承:

<?php
trait BaseFooTrait
{
    /** @ORM'Column(name="foo", type="string") */
    protected $foo;
    // ... getter and setter
}
/**
 * @ORM'Entity
 */
class Base
{
    /** @ORM'Column(name="bar", type="string") */
    protected $bar;
    // ... getter and setter
}
/**
 * @ORM'Entity
 */
class FooBase extends Base
{
    use BaseFooTrait;
}

=> 扩展Base你没有foo.