Doctrine2多级单表继承(未知列)

时间:2015-09-01 12:47:20

标签: mysql symfony doctrine-orm single-table-inheritance

我不确定这是我的代码中的错误还是错误:

<?php
namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({
 *      "customer" = "Customer", 
 *      "projectManager" = "ProjectManager", 
 *      "developer" = "Developer"
 * })
 */
abstract class Person
{
    // ...
}

/**
 * @Entity
 */
class Customer extends Person
{
    // ...
}

/**
 * @Entity
 */
abstract class Employee extends Person
{
    /**
     * @var boolean
     * @Column(name="retired", type="boolean", nullable=true)
     */
    protected $retired;
}

/**
 * @Entity
 */
class ProjectManager extends Employee
{
    // ...
}

/**
 * @Entity
 */
class Developer extends Employee
{
    // ...
}

尝试更新数据库时app/console doctrine:schema:update --force Doctrine忽略Employee :: retired属性,应用程序将导致错误:未知列'已退役'

如果我手动创建了列,一切都按预期工作。此外,这只发生在单个表中,因此如果我切换到类表继承,则在模式更新时创建该列。

2 个答案:

答案 0 :(得分:1)

你不能跳过&#39;对于最低级别的继承,您必须描述每个级别。

在leat它适用于@InheritanceType("JOINED"),我猜测它与SINGLE_TABLE相同。

<?php
namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({
 *      "customer"       = "Customer", 
 *      "employee"       = "Employee",
 *      "projectManager" = "ProjectManager", 
 *      "developer"      = "Developer"
 * })
 */
abstract class Person
{
    // ...
}

/**
 * @Entity
 */
class Customer extends Person
{
    // ...
}

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({
 *      "employee"       = "Employee", 
 *      "projectManager" = "ProjectManager", 
 *      "developer"      = "Developer"
 * })
 */
abstract class Employee extends Person
{
    /**
     * @var boolean
     * @Column(name="retired", type="boolean", nullable=true)
     */
    protected $retired;
}

/**
 * @Entity
 */
class ProjectManager extends Employee
{
    // ...
}

/**
 * @Entity
 */
class Developer extends Employee
{
    // ...
}

答案 1 :(得分:0)

您无需在根实体的鉴别器映射中映射中间抽象实体,以使其正常工作。只是不要将其设为@Entity,而是@MappedSuperclass

在您的情况下,Employee应该是@MappedSuperclass,并且所有扩展 Employee和地图中显示的@Entity