Class Table Inheritance is being used to extend Person, to allow for the addition of Employee and Dragon.
A Person, Employee and Dragon can all have a Toys.
How should the connection between Person, Employee, Dragon and Toys be made?
The advantage of Class Table Inheritance seems to be that the relationship can be set on the Parent Entity(Person) avoiding the creation of new Entities called: PersonToy, EmployeeToy and DragonToy.
It would be ideal if code could be written so that $toy->setEmployee($employee)
, $toy->setDragon($dragon)
all worked, internally I assume it must have to do setPerson()
.
On the opposite side would it be possible to call $toy->getDragon() and $toy->getEmployee() and get a Dragon/Employee and not a Person
I understand that the Entity Toy only has a relationship with Person, but a Person could be extended and therefore be a Dragon or Employee.
How could this be achieved if possible?
/**
* @Entity
* @ORM\Table(name="perosn")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee", "dragon" = "Dragon"})
*/
class Person
{
/**
* @ORM\OneToMany(targetEntity="Toy", mappedBy="person")
*/
protected $toys;
}
/**
* @Entity
* @ORM\Table(name="employee")
*/
class Employee extends Person
{
// ...
}
/**
* @Entity
* @ORM\Table(name="dragon")
*/
class Dragon extends Person
{
// ...
}
/**
* @Entity
* @ORM\Table(name="toy")
*/
class Toy {
/**
* @ORM\ManyToOne(targetEntity="Person", inversedBy="toys", cascade={"persist"})
*/
protected $person;
}