php - Am I designing my Symfony Entities the right way?
Solution:
Why not just make your base Entity Product as abstract? Then all your other Products can just extend it?
Abstract:
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
abstract class Product
{
Product1:
/**
* @ORM\Entity(repositoryClass=Product1Repository::class)
*/
class Product1 extends Product
{
Answer
Solution:
You could check Class Table Inheritance from Doctrine docs.
Basically you create your base Entity with all the fields that are common to sub-entities, then you create a discriminator column and you map the values this column can have, which will map to your sub-entities.
<?php
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/** @Entity */
class Employee extends Person
{
// ...
}
In this way you'll have a table Person with a column named "discr" and all the other fields you declarated; and a table called Employee with Person.id as Primary Key and Foreign key from Person, plus all the fields you declarated in "Employee" entity.
Source