neoDSI   CTO & Développeur back-end symfony
+33 6 62 75 87 89 Contact

ORM Doctrine2 héritage de table

L’idée

Considérons la description d’entreprise publique et privé.

Les entreprises publiques ont un ministère de rattachement alors que les entreprises privés ont un chiffre d’affaire. Les deux ont en commun, un nom et un pays. On doit pouvoir connaitre le type (privé ou public) et c’est ce type qui sera utilisé comme discriminant.

Donc nous aurons une table company, une table company_private et une table company_public

Table company

/**
 * @ORM\Entity
 * @ORM\Table(name="company")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"public" = "CompanyPublic", "private" = "CompanyPrivate"})
 */
abstract class Company
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $pays;

    abstract public function getType();

Table company_private

/**
 * @ORM\Entity
 * @ORM\Table(name="company_private")
 */
class CompanyPrivate extends Company
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $ca;

    public function getType()
    {
        return 'private';
    }

Table company_public

/**
 * @ORM\Entity
 * @ORM\Table(name="company_public")
 */
class CompanyPublic extends Company
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $ministere;

    public function getType()
    {
        return 'public';
    }

Attention, dans les tables company_private et company_public il ne doit pas y avoir de getId.