Jump to content

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
https://forums.phpfreaks.com/topic/308314-doctrine-class-table-inheritance-help/
Share on other sites

On 2/12/2019 at 11:30 AM, requinix said:

Try inserting some data and see what gets stored.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.