Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 实体继承主义_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php 实体继承主义

Php 实体继承主义,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我有一个实体,我想将其用作其他实体的基类(目前未知),我需要在基类实体中存储关系: /** * @ORM\Entity * @ORM\Table(name="CMS_content") */ class BaseContent { /** * @ORM\ManyToOne(targetEntity="BaseContent") * @ORM\JoinColumn(name="parent", referencedColumnName="id", unique=f

我有一个实体,我想将其用作其他实体的基类(目前未知),我需要在基类实体中存储关系:

/**
 * @ORM\Entity
 * @ORM\Table(name="CMS_content")
 */
class BaseContent {
    /**
     * @ORM\ManyToOne(targetEntity="BaseContent")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id", unique=false)
     */
    protected $parent;

    /**
     * @ORM\ManyToOne(targetEntity="ContentType")
     * @ORM\JoinColumn(name="content_type", referencedColumnName="id", unique=false)
     */
    protected $contentType;
    ...
};

/**
 * @ORM\Entity
 * @ORM\Table(name="CMS_whateverSpecializedContent")
 */
class WhateverSpecializedContent extends BaseContent {};
我不能使用
@ORM\InheritanceType(“JOINED”)
,因为我希望以后能够创建任意数量的子类,而不涉及基类。我还需要将基类放在一个单独的数据库表中,这样关系才有意义


还有什么其他选项可以管理这种结构?

我没有使用实体继承,而是使用委托设计模式。
Content
BaseContent
都实现了一个公共接口,
BaseContent
将功能委托给一个连接的内容实体

现在,这个BaseContent的每个子类都将有一个连接的内容实体,可以在需要IContent的地方使用

interface IContent {...}

/**
 * @ORM\Entity
 * @ORM\Table(name="CMS_content")
 */
class Content implements IContent {
    /**
     * @ORM\ManyToOne(targetEntity="BaseContent")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id", unique=false)
     */
    protected $parent;

    /**
     * @ORM\ManyToOne(targetEntity="ContentType")
     * @ORM\JoinColumn(name="content_type", referencedColumnName="id", unique=false)
     */
    protected $contentType;
    ...
};

/**
 * @ORM\Entity
 * @ORM\Table(name="CMS_whateverSpecializedContent")
 */
class WhateverSpecializedContent extends BaseContent {};

/**
 * @ORM\MappedSuperclass
 */
abstract class BaseContent implements IContent {
    /**
     * @ORM\OneToOne(targetEntity="Content", cascade={"persist", "merge", "remove"})
     * @ORM\JoinColumn(name="content", referencedColumnName="id", unique=false)
     */
    private $content;

    public function implementedMethod() {
        $this->content->implementedMethod();
    }
};

在这里,您将使用多表继承方法。不确定为什么不想手动更新基类?@busypeoples基类是通用cms捆绑包的一部分,可以在多个项目中重用,而无需多个版本,我希望它通过定义专用实体类型从其他捆绑包扩展实体。基本实体不应该知道它的子类。您可能需要阅读以下内容: