LexikTranslationBundle icon indicating copy to clipboard operation
LexikTranslationBundle copied to clipboard

Extend TransUnit entity by declaring new relation with a new Entity

Open redjanym opened this issue 9 years ago • 4 comments

Hello,

i am trying to modify the entities of the bundle by adding one new Entity called TransUnitLocation which must have a Many-To-Many relation with TransUnit. I am creating a new bundle, declaring it as the Child of LexiTranslationBundle and after creating the new TransUnitLocation entity i am modifying the MappedSuperclass configuration file of the TransUnit entity by declaring the new Many-To-Many Unidirectional relation all according to the Doctrine Inheritance Mapping specidfications. like this: Owing Side

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 

    <mapped-superclass name="MyBundle\Model\TransUnit">

        <field name="key" column="key_name" type="string" length="255" />

        <field name="domain" column="domain" type="string" length="255" />

        <field name="createdAt" column="created_at" type="datetime" nullable="true" />

        <field name="updatedAt" column="updated_at" type="datetime" nullable="true" />

        <many-to-one field="transUnitLocations" inversed-by="transUnits" target-entity="MyBundle\Entity\TransUnitLocation" />
    </mapped-superclass>
</doctrine-mapping>

New Entity

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 

    <mapped-superclass name="MyBundle\Model\TransUnitLocation">
        <many-to-one target-entity="MyBundle\Entity\TransUnit" field="transUnits" inversed-by="transUnitLocations">
            <join-column name="trans_unit_location_id" referenced-column-name="id" />
        </many-to-one>
    </mapped-superclass>
</doctrine-mapping>

On updating the schema i get the error: [Doctrine\DBAL\Schema\SchemaException]
The table with name 'database.lexik_trans_unit' already exists.

Any ideas?

Thanks.

redjanym avatar Mar 01 '16 11:03 redjanym

Hi, sorry for the late reply, but did you try to override the TransUnit entity instead of the mapped super class ?

cedric-g avatar Mar 15 '16 08:03 cedric-g

According to Doctrine, it is not possible to override an entity because it means that there will be 2 entities, one with the new fields and one without it. The only way is to provide a Mapped Superclass(as a Base class) or override the provided one. That's what i'm doing in this case, by following the doctrine instructions here

redjanym avatar Mar 15 '16 09:03 redjanym

Hum yes you're right I forgot that point, so the best would be we only provide some super-classes/interfaces and leave people implements final entities.

cedric-g avatar Mar 31 '16 08:03 cedric-g

You have already done that, there are superclasses for each entity in the Lexik\Bundle\TranslationBundle\Model folder. but still when i override one of the superclasses i need to explicitly declare the new superclass in the entity where i extend it. Foe example, i declare a new TranslationUnit abstract class with some new relations but i still have to extend the TranslationUnit entity with this new abstract class, so i must still override the entity. Any ideas?

redjanym avatar Mar 31 '16 08:03 redjanym