In Doctrine 2 ORM I have an abstract "Communication" entity that uses SINGLE_TABLE inheritance to decide which type of communication it is:
Communication.orm.yaml:
App\Entity\Communication:
type: entity
table: communication
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: mode
type: string
discriminatorMap:
push: App\Entity\Communication\Push
notice: App\Entity\Communication\Notice
email: App\Entity\Communication\Email
# ...
So there are 3 communication types (push, notice, and email) that all use the "communication" table.
Each "Communication" is translatable. Previously, I had 3 entities containing these translations. Each of these i18n entities uses the same "i18n_communication" table, and contains a bi-directional many-to-one relationship with their respective type of Communication:
- App\Entity\I18n\Communication\Push
- App\Entity\I18n\Communication\Notice
- App\Entity\I18n\Communication\Email
Since having multiple entities using the same table without table inheritance is illegal in Doctrine 2, I made all of the I18n entities extend a base I18nCommunication class, similar to the non-i18n entities that they translate:
I18nCommunication.orm.yaml:
App\Entity\I18N\Communication\I18nCommunication:
type: entity
table: i18n_communication
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: communication.mode ******
type: string
discriminatorMap:
push: App\Entity\I18N\Communication\Push
notice: App\Entity\I18N\Communication\Notice
email: App\Entity\I18N\Communication\Email
# ...
This doesn't work. All entities that extend I18nCommunication have a "communication" field that references the Communication entity they translate, but Doctrine doesn't know that what to do with the "name: communication.mode" line.
My question is this: How do I use a field from a related entity as the discriminator column?
I tried using inheritanceType: JOINED and extending I18nCommunication from Communication so I had access to the parent's mode field. However, that doesn't work because of Communication's inheritanceType: SINGLE_TABLE, it thinks that I18nCommunication should then reside in the communication table.
Is there a way to do this? Or will I have to resort to combining my I18nCommunication entities into a single entity?
Thank you.
How do I use a field from a related entity as the discriminator column?
why do you need such requirement, it doesn't make any sense to me. – emix