0

i have this situation

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

When i generate entities i add the inheritance

class User {
 // Some code...
}

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

The problem appears whe i try to update database schema because of the same "id" column name of both the entities. I've tryied to add an attribute override but i guess this is not the proper way

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

I've tryied also to change the property name (not the column name) in guest_id but the error is the same

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

Do i have to change the Guest "id" column name? Where am i wrong?

Thank You

Jack Skeletron
  • 1,351
  • 13
  • 37
  • 1
    I think you can remove id from entity Guest ! Have a look to http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html Why have you choose XML for mapping ? Strange choice ?!? – Weenesta - Mathieu Dormeval Nov 25 '16 at 15:06
  • @MathieuDormeval i've removed from xml the id as you suggested. Doctrine generated an id field in the db schema. I was worried about all the entities that kept a reference to the Guest entity (table) through the guest.id It seems also that all these entities had no problem because of the guest.id field generated by the ORM. I guess (hope) this is the right way. – Jack Skeletron Nov 25 '16 at 15:40
  • OK make your tests, I you have some other problem, tell us !! – Weenesta - Mathieu Dormeval Nov 25 '16 at 15:46
  • @MathieuDormeval thank you very much! About the XML choice, it's because is more readable to me, with Eclipse i can add node and attributes by a design interface. Should i use Annotations? Do annotations improve performance? – Jack Skeletron Nov 25 '16 at 15:54
  • No, but Annotations and YAML is the most current choices... Do you want me to do an correct answer ? – Weenesta - Mathieu Dormeval Nov 25 '16 at 15:56
  • @MathieuDormeval do you mean adding an answer to this question so i can add a +1? Sure... Sorry i'm new on stack. – Jack Skeletron Nov 25 '16 at 16:05
  • This will mark it solved and answered, other people can find what they search, that why this website exists ! I will make a fully correct answer when I have time. – Weenesta - Mathieu Dormeval Nov 25 '16 at 16:14
  • @MathieuDormeval thank You again! – Jack Skeletron Nov 25 '16 at 16:17

1 Answers1

0

To make the status of this question to answered and from 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>

You must delete the primary key on inherited table, because it's already declared on the mother table.