DDC-854: Multiple discrimination levels #1058

Closed
opened 2026-01-22 13:00:29 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 2, 2010).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user xphere81:

When I define the following classes

 * @entity
 * @inheritanceType("JOINED")
 * @discriminatorColumn(name="type", type="integer")
 * @discriminatorMap({
 *     0 = "SubClass1",
 *     1 = "SubClass2"
 * })
 */
abstract class BaseClass
{
    /*** @Id @GeneratedValue @Column(type="integer") **/
    private $id;
    /*** @Column **/
    private $baseclass_field;
}

/****
 * @entity
 */
class SubClass1 extends BaseClass
{
    /*** @Column **/
    private $subclass1_field;
}

/****
 * @entity
 * @inheritanceType("SINGLE_TABLE")
 * @discriminatorColumn(name="type", type="integer")
 * @discriminatorMap({
 *     0 = "SubClass3",
 *     1 = "SubClass4"
 * })
 */
abstract class SubClass2 extends BaseClass
{
    /*** @Column **/
    private $subclass2_field;
}

/****
 * @entity
 */
class SubClass3 extends SubClass2
{
    /*** @Column **/
    private $subclass3_field;
}

/****
 * @entity
 */
class SubClass4 extends SubClass2
{
    /*** @Column **/
    private $subclass4_field;
}```

then use `doctrine orm:schema-tool:update --dump-sql` the result is:

```CREATE TABLE BaseClass (id INT AUTO_INCREMENT NOT NULL,
                        baseclass_field VARCHAR(255) NOT NULL,
                        type INT NOT NULL,
                        PRIMARY KEY(id)) ENGINE = InnoDB;

CREATE TABLE SubClass1 (id INT AUTO_INCREMENT NOT NULL,
                        subclass1_field VARCHAR(255) NOT NULL,
                        PRIMARY KEY(id)) ENGINE = InnoDB;

CREATE TABLE SubClass2 (id INT AUTO_INCREMENT NOT NULL,
                        baseclass_field VARCHAR(255) NOT NULL,
                        subclass2_field VARCHAR(255) NOT NULL,
                        type INT NOT NULL,
                        subclass3_field VARCHAR(255) NOT NULL,
                        subclass4_field VARCHAR(255) NOT NULL,
                        PRIMARY KEY(id)) ENGINE = InnoDB;

ALTER TABLE SubClass1
    ADD FOREIGN KEY (id) REFERENCES BaseClass(id)
    ON DELETE CASCADE```

Which I think it's wrong because:
# `id` field in `SubClass2` is not defined as a foreign key to `BaseClass`
# `baseclass_field` is duplicated in `SubClass2`
# `id` fields in `SubClass1` and `SubClass2` must not have `AUTO_INCREMENT`

See [this question on SO](http://stackoverflow.com/questions/3932505/multiple-discrimination-levels-while-using-doctrine2) for a more developed example.
Originally created by @doctrinebot on GitHub (Nov 2, 2010). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user xphere81: When I define the following classes `````` /**** * @entity * @inheritanceType("JOINED") * @discriminatorColumn(name="type", type="integer") * @discriminatorMap({ * 0 = "SubClass1", * 1 = "SubClass2" * }) */ abstract class BaseClass { /*** @Id @GeneratedValue @Column(type="integer") **/ private $id; /*** @Column **/ private $baseclass_field; } /**** * @entity */ class SubClass1 extends BaseClass { /*** @Column **/ private $subclass1_field; } /**** * @entity * @inheritanceType("SINGLE_TABLE") * @discriminatorColumn(name="type", type="integer") * @discriminatorMap({ * 0 = "SubClass3", * 1 = "SubClass4" * }) */ abstract class SubClass2 extends BaseClass { /*** @Column **/ private $subclass2_field; } /**** * @entity */ class SubClass3 extends SubClass2 { /*** @Column **/ private $subclass3_field; } /**** * @entity */ class SubClass4 extends SubClass2 { /*** @Column **/ private $subclass4_field; }``` then use `doctrine orm:schema-tool:update --dump-sql` the result is: ```CREATE TABLE BaseClass (id INT AUTO_INCREMENT NOT NULL, baseclass_field VARCHAR(255) NOT NULL, type INT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE SubClass1 (id INT AUTO_INCREMENT NOT NULL, subclass1_field VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE SubClass2 (id INT AUTO_INCREMENT NOT NULL, baseclass_field VARCHAR(255) NOT NULL, subclass2_field VARCHAR(255) NOT NULL, type INT NOT NULL, subclass3_field VARCHAR(255) NOT NULL, subclass4_field VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; ALTER TABLE SubClass1 ADD FOREIGN KEY (id) REFERENCES BaseClass(id) ON DELETE CASCADE``` Which I think it's wrong because: # `id` field in `SubClass2` is not defined as a foreign key to `BaseClass` # `baseclass_field` is duplicated in `SubClass2` # `id` fields in `SubClass1` and `SubClass2` must not have `AUTO_INCREMENT` See [this question on SO](http://stackoverflow.com/questions/3932505/multiple-discrimination-levels-while-using-doctrine2) for a more developed example. ``````
admin added the New Feature label 2026-01-22 13:00:29 +01:00
admin closed this issue 2026-01-22 13:00:30 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 3, 2010):

Comment created by @beberlei:

This is currently not possible for both technical and performance reasons. We would have to discuss this in more detail, but i am not even sure this is feasible and desirable implementation wise. Roman?

@doctrinebot commented on GitHub (Nov 3, 2010): Comment created by @beberlei: This is currently not possible for both technical and performance reasons. We would have to discuss this in more detail, but i am not even sure this is feasible and desirable implementation wise. Roman?
Author
Owner

@doctrinebot commented on GitHub (Oct 4, 2011):

Comment created by @guilhermeblanco:

This cannot be fixable.

If you have multiple DiscriminatorMap levels, how would it work if you fetch an instance of a deep class instance referring to the root node? Example:

class A { ... }

class B extends A { ... }

class C extends B { ... }

// Suppose C (id=1)
// How would this work:

$entity = $em->find('A', 1); // It should be C instance, but it is not mapped in A DiscriminatorMap 

Since we cannot fix this issue, I'm marking the issue as can't fix.

@doctrinebot commented on GitHub (Oct 4, 2011): Comment created by @guilhermeblanco: This cannot be fixable. If you have multiple DiscriminatorMap levels, how would it work if you fetch an instance of a deep class instance referring to the root node? Example: ``` class A { ... } class B extends A { ... } class C extends B { ... } // Suppose C (id=1) // How would this work: $entity = $em->find('A', 1); // It should be C instance, but it is not mapped in A DiscriminatorMap ``` Since we cannot fix this issue, I'm marking the issue as can't fix.
Author
Owner

@doctrinebot commented on GitHub (Oct 4, 2011):

Issue was closed with resolution "Can't Fix"

@doctrinebot commented on GitHub (Oct 4, 2011): Issue was closed with resolution "Can't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1058