Doctrine2 Class Table Inheritance and Cascading De

2019-06-11 08:39发布

I'm working with Doctrine2.2.2 in conjunction with Symfony 2.0.15. I notice that when using Class Table Inheritance, for example:

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
 */

this will create a foreign key cascading delete constraint on child entities/tables (in this case Employee). Doctrine's documentation on this contains an important-looking yellow box that reads:

When you do not use the SchemaTool to generate the required SQL you should know that deleting a class table inheritance makes use of the foreign key property ON DELETE CASCADE in all database implementations. A failure to implement this yourself will lead to dead rows in the database.

Which doesn't make sense to me. Does this mean that if you don't use the SchemaTool then Doctrine will create the foreign key cascading delete constraint? If one did use the SchemaTool, would Doctrine use it's built-in cascade capabilities instead?

1条回答
我命由我不由天
2楼-- · 2019-06-11 09:06

What is is saying is that when you generate the SQL with the SchemaTool, it will also add the appropriate ON DELETE CASCADE part to your foreign key constraint.

ALTER TABLE Employee ADD CONSTRAINT FK_55D6C234BF396750 
    FOREIGN KEY (id) REFERENCES Parent(id) ON DELETE CASCADE;

If you don't use the SchemaTool, you need to make sure that the foreign key constraint has the ON DELETE CASCADE part, or when you delete rows from the Employee table you will end up with orphaned rows in your parent table.

查看更多
登录 后发表回答