Doctrine2类表继承实体ID

时间:2016-11-25 15:01:32

标签: symfony doctrine-orm class-table-inheritance

我有这种情况

<entity name="Company\Entity\User\User" table="usr_user" inheritance-type="JOINED">
    <unique-constraints>
        <unique-constraint name="username_UNIQUE" columns="username" />
    </unique-constraints>
    <discriminator-column name="discriminator" />
    <discriminator-map>
        <discriminator-mapping value="guest" class="\Company\Entity\User\Guest"></discriminator-mapping>
        <discriminator-mapping value="user" class="\Company\Entity\User\User"></discriminator-mapping>
    </discriminator-map>
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO" />
    </id>
    <field name="username" type="string" column="username" length="255" />
    <field name="password" type="string" column="password" length="50" />
</entity>

<entity name="Company\Entity\User\Guest" table="usr_guest">
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO" />
    </id>
    <field name="additionalField1" type="string" column="additional_field_1" length="255" />
    <field name="additionalField1" type="string" column="additional_field_2" length="255" />
</entity>

当我生成实体时,我添加了继承

class User {
 // Some code...
}

class Guest extends User{
 // Some code...
}

当我尝试更新数据库架构时出现问题,因为相同的&#34; id&#34;两个实体的列名。我试图添加一个属性覆盖,但我想这不是正确的方法

<entity name="Company\Entity\User\Guest" table="usr_guest">

    <id name="id" type="integer" column="id">
        <generator strategy="AUTO" />
    </id>
    <field name="additionalField1" type="string" column="additional_field_1" length="255" />
    <field name="additionalField1" type="string" column="additional_field_2" length="255" />
    <attribute-override name="id">
                <field column="guest_id" />
            </attribute-override>
</entity>

我也尝试过更改guest_id中的属性名称(不是列名),但错误是相同的

 [Doctrine\ORM\Mapping\MappingException]
  Duplicate definition of column 'id' on entity 'Company\Entity\User\Guest' in a field or discriminator column mapping.

我是否必须更改Guest&#34; id&#34;专栏名称? 我哪里错了?

谢谢

1 个答案:

答案 0 :(得分:0)

要从this

中回答此问题的状态
<entity name="Company\Entity\User\Guest" table="usr_guest">
    <field name="additionalField1" type="string" column="additional_field_1" length="255" />
    <field name="additionalField1" type="string" column="additional_field_2" length="255" />
    <attribute-override name="id">
      <field column="guest_id" />
    </attribute-override>
</entity>

<entity name="Company\Entity\User\User" table="usr_user" inheritance-type="JOINED">
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO" />
    </id>
    <unique-constraints>
        <unique-constraint name="username_UNIQUE" columns="username" />
    </unique-constraints>
    <discriminator-column name="discriminator" />
    <discriminator-map>
        <discriminator-mapping value="guest" class="\Company\Entity\User\Guest"></discriminator-mapping>
        <discriminator-mapping value="user" class="\Company\Entity\User\User"></discriminator-mapping>
    </discriminator-map>
    <field name="username" type="string" column="username" length="255" />
    <field name="password" type="string" column="password" length="50" />
</entity>

您必须删除继承表上的主键,因为它已在母表上声明。