Doctrine different values of DiscriminatorMap for the same subclass #5001

Closed
opened 2026-01-22 14:56:13 +01:00 by admin · 5 comments
Owner

Originally created by @mayconfsbrito on GitHub (Feb 1, 2016).

Originally assigned to: @Ocramius on GitHub.

I need to extend a Doctrine Entity with single table inherit with DiscriminatorColumn and DiscriminatorMap from different values to the same subclass.

At the moment I'm trying to do of this way, but just the last value of each Entity was applying. I sought for other ways to do this, with arrays, or collections, but I found nothing in the Doctrine or Symphony Documentation. See the annotation code below:

use Doctrine\ORM\Mapping as ORM;

/**
 * Classe abstrata para definição de um objeto produto
 * 
 * @author Maycon Brito
 * @package  models/Doctrine/Entidades
 * @subpackage Produtos
 *
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="codigosubtipo", type="integer")
 * @ORM\DiscriminatorMap({
 *     "3" = "ProdutoAcabado",
 *     "12" = "ProdutoMarketing",
 *     "34" = "ProdutoMarketing",
 *     "36" = "ProdutoEmbalagem", 
 *     "46" = "ProdutoMarketing",
 *     "59" = "ProdutoEmbalagem", 
 *     "60" = "ProdutoEmbalagem", 
 *     "61" = "ProdutoEmbalagem", 
 *     "62" = "ProdutoEmbalagem", 
 *     "81" = "ProdutoEmbalagem", 
 *     "82" = "ProdutoMarketing",
 *     "83" = "ProdutoMarketing",
 *     "84" = "ProdutoMarketing",
 *     "86" = "ProdutoMarketing",
 *     "87" = "ProdutoMarketing",
 *     "89" = "ProdutoMarketing", 
 *     "90" = "ProdutoMarketing",
 *     "91" = "ProdutoMarketing",
 *     "92" = "ProdutoMarketing",
 *     "93" = "ProdutoMarketing",
 *     "94" = "ProdutoMarketing",
 *     "95" = "ProdutoMarketing",
 *     "96" = "ProdutoMarketing",
 *     "97" = "ProdutoMarketing",
 *     "98" = "ProdutoMarketing",
 *     "99" = "ProdutoMarketing",
 *     "100" = "ProdutoMarketing",
 *     "101" = "ProdutoMarketing",
 *     "102" = "ProdutoMarketing",
 *     "103" = "ProdutoEmbalagem", 
 *     "104" = "ProdutoEmbalagem", 
 *     "105" = "ProdutoAcabado",
 *     "107" = "ProdutoAcabado",
 *     "108" = "ProdutoEmbalagem", 
 *     "109" = "ProdutoEmbalagem", 
 *     "111" = "ProdutoEmbalagem", 
 *     "112" = "ProdutoEmbalagem", 
 *     "128" = "ProdutoEmbalagem",
 *     "123" = "ProdutoAcabado",
 *     "106" = "ProdutoMarketing",
 *     "110" = "ProdutoMarketing",
 *     "121" = "ProdutoMarketing",
 *     "125" = "ProdutoTerceirizado",
 *     "126" = "ProdutoMarketing",
 *     "127" = "ProdutoTerceirizado"
 * })
 * @ORM\Table(name="produtos", uniqueConstraints={@ORM\UniqueConstraint(name="id", columns={"id"})}, indexes={@ORM\Index(name="IDX_CodigoSubTipo", columns={"CodigoSubTipo"}), @ORM\Index(name="IDX_CodigoUnidade", columns={"CodigoUnidade"}), @ORM\Index(name="IDX_Volume", columns={"CodigoVolume"}), @ORM\Index(name="IDX_Classe", columns={"CodigoClasse"}), @ORM\Index(name="IDX_CompraAtivada", columns={"CompraAtivada"}), @ORM\Index(name="IDX_Linha", columns={"id_Linha"}), @ORM\Index(name="IDX_LinhaEnvase", columns={"id_LinhaEnvase"}), @ORM\Index(name="idx_Finalidade", columns={"Finalidade"}), @ORM\Index(name="IDX_CodigoProdutoRend", columns={"CodigoProdutoRend"})})
 * @ORM\Entity
 */
abstract class Produto implements InterfaceProduto {

So by example, when I sought for all "ProdutoMarketing" entities the result was just the "ProdutoMarketing" that the value of "codigosubtipo" column was 126.

Another possible Solution:

The discriminatorcolumn (codigosubtipo) is a foreign key of another table (subtipo). This table has a column that isn't an id, but your values are determinats to define the inherited subclass above. If was possible to define the DiscrimnatorColumn of a inner join of a column of another table this problem will be solved.

OBS: I open this question on StackOverflow some days ago.

Originally created by @mayconfsbrito on GitHub (Feb 1, 2016). Originally assigned to: @Ocramius on GitHub. I need to extend a Doctrine Entity with single table inherit with DiscriminatorColumn and DiscriminatorMap from different values to the same subclass. At the moment I'm trying to do of this way, but just the last value of each Entity was applying. I sought for other ways to do this, with arrays, or collections, but I found nothing in the Doctrine or Symphony Documentation. See the annotation code below: ``` php use Doctrine\ORM\Mapping as ORM; /** * Classe abstrata para definição de um objeto produto * * @author Maycon Brito * @package models/Doctrine/Entidades * @subpackage Produtos * * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="codigosubtipo", type="integer") * @ORM\DiscriminatorMap({ * "3" = "ProdutoAcabado", * "12" = "ProdutoMarketing", * "34" = "ProdutoMarketing", * "36" = "ProdutoEmbalagem", * "46" = "ProdutoMarketing", * "59" = "ProdutoEmbalagem", * "60" = "ProdutoEmbalagem", * "61" = "ProdutoEmbalagem", * "62" = "ProdutoEmbalagem", * "81" = "ProdutoEmbalagem", * "82" = "ProdutoMarketing", * "83" = "ProdutoMarketing", * "84" = "ProdutoMarketing", * "86" = "ProdutoMarketing", * "87" = "ProdutoMarketing", * "89" = "ProdutoMarketing", * "90" = "ProdutoMarketing", * "91" = "ProdutoMarketing", * "92" = "ProdutoMarketing", * "93" = "ProdutoMarketing", * "94" = "ProdutoMarketing", * "95" = "ProdutoMarketing", * "96" = "ProdutoMarketing", * "97" = "ProdutoMarketing", * "98" = "ProdutoMarketing", * "99" = "ProdutoMarketing", * "100" = "ProdutoMarketing", * "101" = "ProdutoMarketing", * "102" = "ProdutoMarketing", * "103" = "ProdutoEmbalagem", * "104" = "ProdutoEmbalagem", * "105" = "ProdutoAcabado", * "107" = "ProdutoAcabado", * "108" = "ProdutoEmbalagem", * "109" = "ProdutoEmbalagem", * "111" = "ProdutoEmbalagem", * "112" = "ProdutoEmbalagem", * "128" = "ProdutoEmbalagem", * "123" = "ProdutoAcabado", * "106" = "ProdutoMarketing", * "110" = "ProdutoMarketing", * "121" = "ProdutoMarketing", * "125" = "ProdutoTerceirizado", * "126" = "ProdutoMarketing", * "127" = "ProdutoTerceirizado" * }) * @ORM\Table(name="produtos", uniqueConstraints={@ORM\UniqueConstraint(name="id", columns={"id"})}, indexes={@ORM\Index(name="IDX_CodigoSubTipo", columns={"CodigoSubTipo"}), @ORM\Index(name="IDX_CodigoUnidade", columns={"CodigoUnidade"}), @ORM\Index(name="IDX_Volume", columns={"CodigoVolume"}), @ORM\Index(name="IDX_Classe", columns={"CodigoClasse"}), @ORM\Index(name="IDX_CompraAtivada", columns={"CompraAtivada"}), @ORM\Index(name="IDX_Linha", columns={"id_Linha"}), @ORM\Index(name="IDX_LinhaEnvase", columns={"id_LinhaEnvase"}), @ORM\Index(name="idx_Finalidade", columns={"Finalidade"}), @ORM\Index(name="IDX_CodigoProdutoRend", columns={"CodigoProdutoRend"})}) * @ORM\Entity */ abstract class Produto implements InterfaceProduto { ``` So by example, when I sought for all "ProdutoMarketing" entities the result was just the "ProdutoMarketing" that the value of "codigosubtipo" column was 126. Another possible Solution: The discriminatorcolumn (codigosubtipo) is a foreign key of another table (subtipo). This table has a column that isn't an id, but your values are determinats to define the inherited subclass above. If was possible to define the DiscrimnatorColumn of a inner join of a column of another table this problem will be solved. OBS: I open this question on StackOverflow some days ago.
admin added the Invalid label 2026-01-22 14:56:13 +01:00
admin closed this issue 2026-01-22 14:56:13 +01:00
Author
Owner

@DHager commented on GitHub (Feb 1, 2016):

I don't think Doctrine supports that. For example, suppose you do this:

$p = new ProdutoMarketing("Olá mundo");
$em->persist($p);

How would Doctrine choose which number? 12, 34, 46, 82, etc.?

@DHager commented on GitHub (Feb 1, 2016): I don't think Doctrine supports that. For example, suppose you do this: ``` PHP $p = new ProdutoMarketing("Olá mundo"); $em->persist($p); ``` How would Doctrine choose which number? 12, 34, 46, 82, etc.?
Author
Owner

@Ocramius commented on GitHub (Feb 1, 2016):

This is indeed not supported. It is simpler to add a different column for the discriminator. Fix the inconsistency via SQL, then add a productType column where you store that integer value.

Closing as Invalid

@Ocramius commented on GitHub (Feb 1, 2016): This is indeed not supported. It is simpler to add a different column for the discriminator. Fix the inconsistency via SQL, then add a `productType` column where you store that integer value. Closing as `Invalid`
Author
Owner

@mayconfsbrito commented on GitHub (Feb 2, 2016):

@DHager the number chosen by doctrine was 126, the last mapped number for the ProdutoMarketing class.

@mayconfsbrito commented on GitHub (Feb 2, 2016): @DHager the number chosen by doctrine was 126, the last mapped number for the ProdutoMarketing class.
Author
Owner

@Ocramius commented on GitHub (Feb 2, 2016):

@mayconfsbrito that's because it will simply pick the last one in the array as per 788143dc03/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php (L2809)

@Ocramius commented on GitHub (Feb 2, 2016): @mayconfsbrito that's because it will simply pick the last one in the array as per https://github.com/doctrine/doctrine2/blob/788143dc0313c7522820ebf6057f73881e7190a3/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L2809
Author
Owner

@mayconfsbrito commented on GitHub (Feb 2, 2016):

@Ocramius thank you for attention. How the Doctrine hasn't a solution in this sense so I will try another.

@mayconfsbrito commented on GitHub (Feb 2, 2016): @Ocramius thank you for attention. How the Doctrine hasn't a solution in this sense so I will try another.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5001