Please wait...

Doctrine table inheritance example

In a previous post, we simply defined what is inheritance in PHP, here we go a little further !!

What is inheritance of tables (or entities) via Doctrine?

Doctrine is an ORM (for Object Relational Mapper), which allows you to define and to link our entities (classes) to build our database, independently of the chosen management system.

That is, we use a class system (as shown in the previous post) to describe the structure of the database

Imagine that we would like to reference a finite set of actions to perform on some existing tables, such as subscribe, unsubscribe, read ...

For example, here is the definition of entity "Actions" (mother) via Doctrine:

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"abonner" = "Abonner", "partager" = "Partager"})
 */
abstract class Actions
{
    /**
     * @Id @GeneratedValue @Column(type="integer")
     * @var int
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }

    /**
     * @var \AppBundle\Entity\User
     */
    private $author;

    /**
     * Set author
     *
     * @param \AppBundle\Entity\User $author
     *
     * @return Action
     */
    public function setAuthor(\AppBundle\Entity\User $author = null)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return \AppBundle\Entity\User
     */
    public function getAuthor()
    {
        return $this->author;
    }
}

Well, we can use the class inheritance methodology, so that some properties or methods are inherited in the "girl" class, or specialize some methods of the "girl" class.

/**
 * @Entity @Table(name="Abonner")
 */
class Abonner extends Actions
{
    /**
     * @var \AppBundle\Entity\Source
     */
    private $source;

    /**
     * Set source
     *
     * @param \AppBundle\Entity\Source $source
     *
     * @return Action
     */
    public function setSource(\AppBundle\Entity\Source $source = null)
    {
        $this->source = $source;

        return $this;
    }

    /**
     * Get source
     *
     * @return \AppBundle\Entity\Source
     */
    public function getSource()
    {
        return $this->source;
    }
}
/**
 * @Entity @Table(name="partager")
 */
class Partager extends Actions
{
    /**
     * @var \AppBundle\Entity\Post
     */
    private $post;

    /**
     * @var \AppBundle\Entity\Cercle
     */
    private $cercle;

    /**
     * Set post
     *
     * @param \AppBundle\Entity\Post $post
     *
     * @return Action
     */
    public function setPost(\AppBundle\Entity\Post $post = null)
    {
        $this->post = $post;

        return $this;
    }

    /**
     * Get post
     *
     * @return \AppBundle\Entity\Post
     */
    public function getPost()
    {
        return $this->post;
    }

    /**
     * Set cercle
     *
     * @param \AppBundle\Entity\Cercle $cercle
     *
     * @return Action
     */
    public function setCercle(\AppBundle\Entity\Cercle $cercle = null)
    {
        $this->cercle = $cercle;

        return $this;
    }

    /**
     * Get cercle
     *
     * @return \AppBundle\Entity\Cercle
     */
    public function getCercle()
    {
        return $this->cercle;
    }
}

We have achieved a entities inheritance.

But what happens at the database level when we do this type of inheritance?

Doctrine will generate us a unique table, in which it will store all the related relationships to our declared entities.

See you soon,

Mathieu