3

Under Symfony 5.0, I use generic entity classes to unify internal projects. My generic entity (e.g. Table) looks like this:


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\TableRepository")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorMap({"generic_table": "App\Entity\Generic\Table", "table": "App\Entity\Table"})
 */
class Site
{
    //protected properties and public methods
}

And inherited class:


use App\Entity\Generic\Table as GenericTable;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\TableRepository")
 */
class Table extends GenericTable
{
    //private properties and public methods
}

However, when executing this command:

php bin/console make:migration

It returns the following:

Table mybd.table already exists.

Even if the table doesn't.

Any idea? Have I forgotten an ORM statement?

  • When using the Discriminator, do not declare a "DiscriminatorMap" on the table (auto generated, [see docs](https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/inheritance-mapping.html), search "if no discriminator" on page). Makes no sense for the parent class to know all of the child classes. If your `Table`/`Site` class should not exist by itself, you might want to make the class `abstract` and use `MappedSuperclass` Annotation on it. Duplicate `Table` due to your discriminator map have `**\Table` twice. – rkeet Jan 29 '20 at 07:50

1 Answers1

0

A lead: we must define different table names on @ORM\Table annotation:

@ORM\Table(name="generic_table") for class Table

@ORM\Table(name="table") for class Table extends GenericTable

Finally, it only creates in database table generic_table and redefines constraints on this table. But I still have issues with Repositories...