Single Table Inheritance - issue #7282

Closed
opened 2026-01-22 15:49:05 +01:00 by admin · 4 comments
Owner

Originally created by @fico7489 on GitHub (Dec 19, 2023).

This is example from documentation:

https://www.doctrine-project.org/projects/doctrine-orm/en/2.17/reference/inheritance-mapping.html#design-time-considerations

#[Entity]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorColumn(name: 'discr', type: 'string')]
#[DiscriminatorMap(['person' => Person::class, 'employee' => Employee::class])]
class Person
{
    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'persons')]
    private Collection $users;
}

#[Entity]
class Employee extends Person
    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'employees')]
    private Collection $users;
}

Is it possible to have both relations with inversedBy like this:

#[Entity]
class User
{
    #[ORM\ManyToMany(targetEntity: Person::class, inversedBy: 'users')]
    private Collection $persons;
    
    #[ORM\ManyToMany(targetEntity: Employee::class, inversedBy: 'users')]
    private Collection $employees;
}

What is suggestions for achieve this?

Originally created by @fico7489 on GitHub (Dec 19, 2023). This is example from documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.17/reference/inheritance-mapping.html#design-time-considerations ``` #[Entity] #[InheritanceType('SINGLE_TABLE')] #[DiscriminatorColumn(name: 'discr', type: 'string')] #[DiscriminatorMap(['person' => Person::class, 'employee' => Employee::class])] class Person { #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'persons')] private Collection $users; } #[Entity] class Employee extends Person #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'employees')] private Collection $users; } ``` Is it possible to have both relations with **inversedBy** like this: ``` #[Entity] class User { #[ORM\ManyToMany(targetEntity: Person::class, inversedBy: 'users')] private Collection $persons; #[ORM\ManyToMany(targetEntity: Employee::class, inversedBy: 'users')] private Collection $employees; } ``` What is suggestions for achieve this?
admin closed this issue 2026-01-22 15:49:05 +01:00
Author
Owner

@greg0ire commented on GitHub (Dec 19, 2023):

This is example from documentation

I'm confused… the page you linked does not mention $users at all, does it? 🤔

@greg0ire commented on GitHub (Dec 19, 2023): > This is example from documentation I'm confused… the page you linked does not mention `$users` at all, does it? :thinking:
Author
Owner

@fico7489 commented on GitHub (Dec 19, 2023):

it is extended example based on documentation example

@fico7489 commented on GitHub (Dec 19, 2023): it is extended example based on documentation example
Author
Owner

@yard-mschwartz commented on GitHub (Dec 21, 2023):

Your example seems bugged to me

  • Why would Employee need private Collection $users; when Person (its parent) already has private Collection $users;?
  • Also, why do you have mappedBy: 'employees' in Person, and mappedBy: 'persons' in Employee?
@yard-mschwartz commented on GitHub (Dec 21, 2023): Your example seems bugged to me * Why would `Employee` need `private Collection $users;` when `Person` (its parent) already has `private Collection $users;`? * Also, why do you have `mappedBy: 'employees'` in `Person`, and `mappedBy: 'persons'` in `Employee`?
Author
Owner

@fico7489 commented on GitHub (Dec 21, 2023):

@yard-mschwartz

thanks for response

yes, you are right it was buggy, I now replaced mappedBy.

yes, Person (its parent) already has private Collection $users; but that relation is not working inside Employee because
mappedBy is persons, not employees

I am looking for some king of mechanism to change mappedBy for that relation inside Employee.

@fico7489 commented on GitHub (Dec 21, 2023): @yard-mschwartz thanks for response yes, you are right it was buggy, I now replaced mappedBy. yes, Person (its parent) already has private Collection $users; but that relation is not working inside Employee because mappedBy is **persons**, not **employees** I am looking for some king of mechanism to change mappedBy for that relation inside Employee.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7282