Association mappings on mapped superclasses #5305

Open
opened 2026-01-22 15:04:01 +01:00 by admin · 0 comments
Owner

Originally created by @sandermarechal on GitHub (Oct 26, 2016).

I would like to propose a feature for the ORM: Allow all association mappings on mapped superclasses, just defer definition of the targetEntity.

Here is my problem. I am writing a library/bundle/module that contains a bunch of abstract classes that represent entities. The user of this library should extend these classes to create their entities. A good practical example is the FOSUserBundle which contains abstract implementations for users and groups.

I want to provide mapping information along with these abstract classes, so I use Mapped Superclass. This allows me to provide mapping information for all fields, but it does not allow mapping information about the assocations between the classes. The target-entity is not known in my library so it is impossible to provide mapping information.

The problem is that an association mapping contains much more information than just the target-entity, and I want to provide mapping for all the other options. I want to specify the join column, database-level cascades, nullability, mapped-by and inversed-by information, etc. Everything except for the target-entity. This is currently impossible. I can only document how the relation should look and hope that the users of my library don't forget anything.

It would be great if I could provide all the information with the mapped superclass. Here is an example using users and groups:

<mapped-superclass name="My\Library\User">
    <many-to-one field="group" inversed-by="users">
        <join-column name="group_id" referenced-column-name="id" on-delete="CASCADE" nullable=false />
    </many-to-one>
</mapped-superclass>

<mapped-superclass name="My\Library\Group">
    <one-to-many field="users" mapped-by="group" />
</mapped-superclass>

A user of this library would only have to provide a very basic mapping:

<entity name="App\User" table="usr">
    <many-to-one field="group" target-entity="App\Group" />
</entity>

<entity name="App\Group" table="grp">
    <one-to-many field="users" target-entity="App\User" />
</entity>

This would make it much easier to use my library and leave less room for bugs in my user's code.

I also suggest that Doctrine checks that the target-entity is not set on a mapped superclass, so this should throw a mapping exception:

<mapped-superclass name="Foo">
    <one-to-many field="foo" target-entity="Bar" mapped-by="..." />
</mapped-superclass>

What do you think? I think it would solve a common problem..

Originally created by @sandermarechal on GitHub (Oct 26, 2016). I would like to propose a feature for the ORM: Allow all association mappings on mapped superclasses, just defer definition of the targetEntity. Here is my problem. I am writing a library/bundle/module that contains a bunch of abstract classes that represent entities. The user of this library should extend these classes to create their entities. A good practical example is the FOSUserBundle which contains abstract implementations for users and groups. I want to provide mapping information along with these abstract classes, so I use Mapped Superclass. This allows me to provide mapping information for all fields, but it does not allow mapping information about the assocations between the classes. The target-entity is not known in my library so it is impossible to provide mapping information. The problem is that an association mapping contains much more information than just the target-entity, and I want to provide mapping for all the other options. I want to specify the join column, database-level cascades, nullability, mapped-by and inversed-by information, etc. Everything except for the target-entity. This is currently impossible. I can only document how the relation should look and hope that the users of my library don't forget anything. It would be great if I could provide all the information with the mapped superclass. Here is an example using users and groups: ``` xml <mapped-superclass name="My\Library\User"> <many-to-one field="group" inversed-by="users"> <join-column name="group_id" referenced-column-name="id" on-delete="CASCADE" nullable=false /> </many-to-one> </mapped-superclass> <mapped-superclass name="My\Library\Group"> <one-to-many field="users" mapped-by="group" /> </mapped-superclass> ``` A user of this library would only have to provide a very basic mapping: ``` xml <entity name="App\User" table="usr"> <many-to-one field="group" target-entity="App\Group" /> </entity> <entity name="App\Group" table="grp"> <one-to-many field="users" target-entity="App\User" /> </entity> ``` This would make it much easier to use my library and leave less room for bugs in my user's code. I also suggest that Doctrine checks that the target-entity is not set on a mapped superclass, so this should throw a mapping exception: ``` xml <mapped-superclass name="Foo"> <one-to-many field="foo" target-entity="Bar" mapped-by="..." /> </mapped-superclass> ``` What do you think? I think it would solve a common problem..
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5305