Jump to content

Doctrine class table inheritance help


NotionCommotion

Recommended Posts

Trying to create a model where Parent1 and Parent2 extend Grandparent, and Child1_1 and Child1_2 extend Parent1 as well as Child2_1 and Child2_2 extend Parent2.

I set up my definitions:

Grandparent:
    type: entity
    table: grandparent_table
    inheritanceType: JOINED
    discriminatorColumn:
        name: discriminator_column_grandparent
        type: string
    repositoryClass: GrandParent
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        GrandParent_Item1:
            type: string
        GrandParent_Item2:
            type: string
Parent1:
    type: entity
    table: parent_table1
    extends: Grandparent
    inheritanceType: JOINED
    discriminatorColumn:
        name: discriminator_column_parent1
        type: string
    id:
        id:
            associationKey: true
    fields:
        Parent1_Item1:
            type: string
        Parent1_Item2:
            type: string
Parent2:
    type: entity
    table: parent_table2
    extends: Grandparent
    inheritanceType: JOINED
    discriminatorColumn:
        name: discriminator_column_parent2
        type: string
    id:
        id:
            associationKey: true
    fields:
        Parent2_Item1:
            type: string
        Parent2_Item2:
            type: string
Child1_1:
    extends: Parent1
    type: entity
    table: child1_1_table
    id:
        id:
            associationKey: true
    fields:
        Child1_1_Item1:
            type: string
        Child1_1_Item2:
            type: string
Child1_2:
    extends: Parent1
    type: entity
    table: child1_2_table
    id:
        id:
            associationKey: true
    fields:
        Child1_2_Item1:
            type: string
        Child1_2_Item2:
            type: string
Child2_1:
    extends: Parent2
    type: entity
    table: child2_1_table
    id:
        id:
            associationKey: true
    fields:
        Child2_1_Item1:
            type: string
        Child2_1_Item2:
            type: string
Child2_2:
    extends: Parent2
    type: entity
    table: child2_2_table
    id:
        id:
            associationKey: true
    fields:
        Child2_2_Item1:
            type: string
        Child2_2_Item2:
            type: string

 And then generate my entities:       

$ vendor/bin/doctrine orm:generate-entities -vvv src

 Processing entity "Child1_1"
 Processing entity "Child1_2"
 Processing entity "Child2_1"
 Processing entity "Child2_2"
 Processing entity "Grandparent"
 Processing entity "Parent1"
 Processing entity "Parent2"


 [OK] Entity classes generated to "/var/www/doctrine/src"

Note that I needed to manually edit the Parent1 and 2 classes to extend Grandparent as well as the individual Child classes to extend their applicable Parent.

Next, I create the schema:

$ vendor/bin/doctrine orm:schema-tool:create --dump-sql

 The following SQL statements will be executed:

     CREATE TABLE child1_1_table (id INT NOT NULL, Child1_1_Item1 VARCHAR(255) NOT NULL, Child1_1_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE child1_2_table (id INT NOT NULL, Child1_2_Item1 VARCHAR(255) NOT NULL, Child1_2_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE child2_1_table (id INT NOT NULL, Child2_1_Item1 VARCHAR(255) NOT NULL, Child2_1_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE child2_2_table (id INT NOT NULL, Child2_2_Item1 VARCHAR(255) NOT NULL, Child2_2_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE grandparent_table (id INT AUTO_INCREMENT NOT NULL, GrandParent_Item1 VARCHAR(255) NOT NULL, GrandParent_Item2 VARCHAR(255) NOT NULL, discriminator_column_grandparent VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE parent_table1 (id INT NOT NULL, Parent1_Item1 VARCHAR(255) NOT NULL, Parent1_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     CREATE TABLE parent_table2 (id INT NOT NULL, Parent2_Item1 VARCHAR(255) NOT NULL, Parent2_Item2 VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
     ALTER TABLE child1_1_table ADD CONSTRAINT FK_3445750EBF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;
     ALTER TABLE child1_2_table ADD CONSTRAINT FK_5AD6F93BF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;
     ALTER TABLE child2_1_table ADD CONSTRAINT FK_DC849CBBF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;
     ALTER TABLE child2_2_table ADD CONSTRAINT FK_3C205356BF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;
     ALTER TABLE parent_table1 ADD CONSTRAINT FK_5386E15BBF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;
     ALTER TABLE parent_table2 ADD CONSTRAINT FK_CA8FB0E1BF396750 FOREIGN KEY (id) REFERENCES grandparent_table (id) ON DELETE CASCADE;

I see how Grandparent has discriminatorColumn discriminator_column_grandparent as expected.

But why doesn't Parent1 and Parent2 have discriminatorColumn's discriminator_column_parent1 and discriminator_column_parent2?

Note that I've also tried making Grandparent, Parent1, and Parent2 abstract but get the same results.  I've also messed around with Mapped Superclasses, but I don't think they are applicable for my use (correct me if you think I am wrong).

Thanks!

Link to comment
Share on other sites

6 hours ago, NotionCommotion said:

Ah, it just stores the name of the final class in the discriminator column, and this column is not a "field" that one would explicitly set.

Right. Each child comes from the parent and each parent comes from the grandparent, and the grandparent table already has a discriminator. That one column can handle all descendants of the grandparent, not just its immediate children.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.