开发者

Polymorphism on Single Table Inheritance in Doctrine 2

开发者 https://www.devze.com 2023-02-18 07:22 出处:网络
For my application, I am thinking of using Single Table Inheritance for a table because it allows me to write specific business logic for each of the subclasses.

For my application, I am thinking of using Single Table Inheritance for a table because it allows me to write specific business logic for each of the subclasses.

However, let's say that some point I want to change the object from one开发者_开发问答 subclass to another. Is this possible? i.e. editing the discriminator column?


Better question than "Is it possible" is "Is it right thing to do"? It is not. Discriminator column is not part of your bussines logic object, it is part of Doctrine2 layer. You may find a way to hack it but it leads to messy code. If you have object 'A' and need object 'B' all you have to do is to destroy A and create B, just like with "normal" not persistent objects. This way your code is more readable an is less coupled with DB layer that you may want to change in a future.


If you needed to change subclasses, you could write a Doctrine migration and use DBAL to migrate the discriminator column (and presumably some other class-specific fields). @smentek is correct in saying the that discriminator column is supposed to be confined to the DB layer.

The issue you have is not really one of polymorphism. It's more related to the need to refactor your database structure and business objects. You probably don't want to changing classes of objects at runtime. However, there's a valid case for refactoring classes as a project progresses.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号