InheritanceType SINGLE_TABLE is not changing target Entity when Dicscriminator changes but not Id #6616

Closed
opened 2026-01-22 15:35:46 +01:00 by admin · 1 comment
Owner

Originally created by @EtienneLancon on GitHub (Jan 28, 2021).

Hi,

I'm quite new to both Doctrine and Symfony so there probably is something i missed.

I'm trying to make a single table inheritance from an abstract class to many children entities. It works pretty well.
But when i request an entity embedding many of these children entities. If both of them have the same Id, it seems Doctrine makes the assumption that this is the same object even if they should not have the same discriminator. For information this is a legacy database and there is no true unique Id.
When i subscribe postLoad event, once embedding entity is loaded, embedded properties that will fail are yet of the wrong proxy type.

Maybe there is a way to disable this assumption process made by Doctrine ?

Or maybe I'm totally wrong, as i said i'm quite new.

Thanks in advance for any help.

Here is the mother class.

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="CHOIXCOD")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="cc_type", type="string", length=3)
 * @ORM\DiscriminatorMap({"PHV" = "Phasevocale",
 *                        "TMS" = "Typemessage",
 *                        "PRI" = "Priorite",
 *                        "ZEL" = "Etatlivraison",
 *                        "ZEM" = "Etatmessage",
 *                        "ZOR" = "Originems"})
 */

abstract class Choixcod
{

    /**
     * @var string|null
     *
     * @ORM\Column(name="CC_CODE", type="string", length=3, nullable=true)
     * @ORM\Id
     */
    protected $ccCode;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CC_LIBELLE", type="string", length=105, nullable=true)
     */
    protected $ccLibelle;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CC_ABREGE", type="string", length=17, nullable=true)
     */
    protected $ccAbrege;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CC_LIBRE", type="string", length=70, nullable=true)
     */
    protected $ccLibre;

    /**
     * @var string|null
     *
     * @ORM\Column(name="CC_TYPE", type="string", length=70, nullable=true)
     */
    protected $ccType;

A child class.

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Etatmessage extends Choixcod{}

And an entity snippet in which it is related.

    /**
     *
     * @ORM\ManyToOne(targetEntity="Etatmessage")
     * @ORM\JoinColumn(name="ZMS_ETATMESSAGE", referencedColumnName="CC_CODE")
     * @ToNull(primaryKey="ccCode")
     */
    private $zmsEtatmessage;
Originally created by @EtienneLancon on GitHub (Jan 28, 2021). Hi, I'm quite new to both Doctrine and Symfony so there probably is something i missed. I'm trying to make a single table inheritance from an abstract class to many children entities. It works pretty well. But when i request an entity embedding many of these children entities. If both of them have the same Id, it seems Doctrine makes the assumption that this is the same object even if they should not have the same discriminator. For information this is a legacy database and there is no true unique Id. When i subscribe postLoad event, once embedding entity is loaded, embedded properties that will fail are yet of the wrong proxy type. Maybe there is a way to disable this assumption process made by Doctrine ? Or maybe I'm totally wrong, as i said i'm quite new. Thanks in advance for any help. Here is the mother class. ``` use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="CHOIXCOD") * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="cc_type", type="string", length=3) * @ORM\DiscriminatorMap({"PHV" = "Phasevocale", * "TMS" = "Typemessage", * "PRI" = "Priorite", * "ZEL" = "Etatlivraison", * "ZEM" = "Etatmessage", * "ZOR" = "Originems"}) */ abstract class Choixcod { /** * @var string|null * * @ORM\Column(name="CC_CODE", type="string", length=3, nullable=true) * @ORM\Id */ protected $ccCode; /** * @var string|null * * @ORM\Column(name="CC_LIBELLE", type="string", length=105, nullable=true) */ protected $ccLibelle; /** * @var string|null * * @ORM\Column(name="CC_ABREGE", type="string", length=17, nullable=true) */ protected $ccAbrege; /** * @var string|null * * @ORM\Column(name="CC_LIBRE", type="string", length=70, nullable=true) */ protected $ccLibre; /** * @var string|null * * @ORM\Column(name="CC_TYPE", type="string", length=70, nullable=true) */ protected $ccType; ``` A child class. ``` use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Etatmessage extends Choixcod{} ``` And an entity snippet in which it is related. ``` /** * * @ORM\ManyToOne(targetEntity="Etatmessage") * @ORM\JoinColumn(name="ZMS_ETATMESSAGE", referencedColumnName="CC_CODE") * @ToNull(primaryKey="ccCode") */ private $zmsEtatmessage; ```
admin closed this issue 2026-01-22 15:35:46 +01:00
Author
Owner

@beberlei commented on GitHub (Feb 7, 2021):

Doctrine single table inheritence requires the ID to be unique across all types of hierarchy. You could make it a composite key over the id and discriminator value.

@beberlei commented on GitHub (Feb 7, 2021): Doctrine single table inheritence requires the ID to be unique across all types of hierarchy. You could make it a composite key over the id and discriminator value.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6616