Symfony ORM inheritance

I have my 3 Entities :

Book, Cd, Dvd

They all 3 shares some properties :

id, title, reference_number...

I want to regroup them into a Parent Class named Document here’s my actual code :

class Cd extends Document
{
    /**
     * @ORMColumn(type="integer")
     */
    private $total_duration;
}


class Dvd extends Document
{

   /**
     * @ORMColumn(type="boolean")
    */
    private $has_bonus;
}

class Novel extends Document
{

   /**
     * @ORMColumn(type="integer")
    */
    private $pages;
}

and my Document class :

abstract class Document
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

   /**
    * @ORMColumn(type="string", length=255)
    */
   private $title;


   /**
    * @ORMColumn(type="string", length=255)
    */
   private $reference_number;
}

To retrieve the ID of my entities I use the Document method :

   protected function getId(): ?int
{
    return $this->id;
}

However I had to implement into my child classes the method :

   public function getId(): ?int
{
    return parent::getId();
}

for some Twig/EasyAdmin reasons.

Is it a good practice or is there another way to build this inheritance? I’m currently dealing with some troubles with the Edit part from EasyAdmin 3 telling me that the Id cannot be retrieved (the reason of the question).
I’m genuinely looking for some docs or whatever that could explain the good pratices. Thanks

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


Giel

Giel

The reason EasyAdmin is giving you this warning, is because you marked your fields as ‘private’. If you would like to have the child class to have access to fields in the inherited class, mark them as protected. Only then they would actually share the properties.