Multiple DiscriminatorColumn with SINGLE_TABLE #5539

Closed
opened 2026-01-22 15:10:28 +01:00 by admin · 8 comments
Owner

Originally created by @Seb33300 on GitHub (May 16, 2017).

Originally assigned to: @lcobucci on GitHub.

I would like to use a discriminator map on multiple fields of my entity but it seems to not be possible.

So I tried to make a discriminator map on multiple levels with Entity inheritance:

/**
 * Task
 *
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *     "type1" = "Type1",
 *     "type1" = "Type2"
 * })
 */
class Task
{
    // ...
}

/**
 * TaskType1
 *
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="subtype", type="string")
 * @ORM\DiscriminatorMap({
 *     "subtype1" = "TaskType1Subtype1"
 * })
 */
class TaskType1 extends Task
{
    // ...
}

/**
 * Type1Subtype1
 *
 * @ORM\Entity
 */
class TaskType1Subtype1 extends TaskType1
{
    // ...
}

When creating a query to get TaskType1Subtype1 entities, this will result in:

SELECT ... FROM Task WHERE subtype IN ('type1', 'subtype1')

instead of

SELECT ... FROM Task WHERE type IN ('type1') AND subtype IN ('subtype1')
Originally created by @Seb33300 on GitHub (May 16, 2017). Originally assigned to: @lcobucci on GitHub. I would like to use a discriminator map on multiple fields of my entity but it seems to not be possible. So I tried to make a discriminator map on multiple levels with Entity inheritance: ```php /** * Task * * @ORM\Entity * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({ * "type1" = "Type1", * "type1" = "Type2" * }) */ class Task { // ... } /** * TaskType1 * * @ORM\Entity * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="subtype", type="string") * @ORM\DiscriminatorMap({ * "subtype1" = "TaskType1Subtype1" * }) */ class TaskType1 extends Task { // ... } /** * Type1Subtype1 * * @ORM\Entity */ class TaskType1Subtype1 extends TaskType1 { // ... } ``` When creating a query to get `TaskType1Subtype1` entities, this will result in: ```sql SELECT ... FROM Task WHERE subtype IN ('type1', 'subtype1') ``` instead of ```sql SELECT ... FROM Task WHERE type IN ('type1') AND subtype IN ('subtype1') ```
admin added the Won't Fix label 2026-01-22 15:10:28 +01:00
admin closed this issue 2026-01-22 15:10:30 +01:00
Author
Owner

@Ocramius commented on GitHub (May 16, 2017):

Mixed inheritance is not supported, and only one discriminator column will ever be considered with the current codebase (that's hardcoded).

What is the use-case?

@Ocramius commented on GitHub (May 16, 2017): Mixed inheritance is not supported, and only one discriminator column will ever be considered with the current codebase (that's hardcoded). What is the use-case?
Author
Owner

@Seb33300 commented on GitHub (May 16, 2017):

I have a task table with type and subtype columns.
Depending on their 2 columns, I need to load a different entity.

Depending on his type and subtype, the task will have different columns (stored in a json column).
I have made something custom to automatically map the data from the json column to the entity properties.

@Seb33300 commented on GitHub (May 16, 2017): I have a task table with `type` and `subtype` columns. Depending on their 2 columns, I need to load a different entity. Depending on his type and subtype, the task will have different columns (stored in a json column). I have made something custom to automatically map the data from the json column to the entity properties.
Author
Owner

@Ocramius commented on GitHub (May 16, 2017):

Yeah, that will not work. What you can do (depending on how advanced your RDBMS is) is creating a read-write view with the discriminator column being a concatenation of the discriminators.

@Ocramius commented on GitHub (May 16, 2017): Yeah, that will not work. What you can do (depending on how advanced your RDBMS is) is creating a read-write view with the discriminator column being a concatenation of the discriminators.
Author
Owner

@Seb33300 commented on GitHub (May 16, 2017):

But doctrine will try to INSERT in the concatenation column which will result in an error.

@Seb33300 commented on GitHub (May 16, 2017): But doctrine will try to INSERT in the concatenation column which will result in an error.
Author
Owner

@Ocramius commented on GitHub (May 16, 2017):

That's typically hijacked with rw views by replacing the insert value and splitting it over two columns, much like with table partitioning strategies.

@Ocramius commented on GitHub (May 16, 2017): That's typically hijacked with rw views by replacing the insert value and splitting it over two columns, much like with table partitioning strategies.
Author
Owner

@Seb33300 commented on GitHub (May 17, 2017):

I am not sure to be able to do that on the database side (SQL Server).

Any chance to do that by adding a listener on the doctrine INSERT to manually split the discriminator field, update the 2 others and remove the discriminator from the query?

(I am using doctrine with Symfony)

@Seb33300 commented on GitHub (May 17, 2017): I am not sure to be able to do that on the database side (SQL Server). Any chance to do that by adding a listener on the doctrine INSERT to manually split the discriminator field, update the 2 others and remove the discriminator from the query? (I am using doctrine with Symfony)
Author
Owner

@Ocramius commented on GitHub (May 17, 2017):

No, that's not going to be possible

@Ocramius commented on GitHub (May 17, 2017): No, that's not going to be possible
Author
Owner

@lcobucci commented on GitHub (Oct 1, 2019):

Closing as Won't fix as per @Ocramius comments.

@lcobucci commented on GitHub (Oct 1, 2019): Closing as `Won't fix` as per @Ocramius comments.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5539