The Doctrine of Multilevel Inheritance

I have four types of products that I would like to map to the Doctrine ORM structure on MySQL DBMS. Products PrepaidProduct , PostpaidProduct , MobilePrepaidProduct , MobilePostpaidProduct with the following structure:

 abstract class Product { /** * @ORM\Column(type="integer") */ private $price; ... } class PrepaidProduct extends Product { /** * @ORM\Column(type="integer") */ private $credit; /** * @ORM\OneToMany(targetEntity="PrepaidDiscount") */ private $prepaidDiscounts; } class PostpaidProduct extends Product { /** * @ORM\OneToMany(targetEntity="BundleMapping") */ private $bundleMappings; } class MobilePrepaidProduct extends PrepaidProduct { /** * @ORM\ManyToOne(targetEntity="Device") */ private $device; } class MobilePostpaidProduct extends PostpaidProduct { /** * @ORM\ManyToOne(targetEntity="Device") */ private $device; } 

The main idea is that I would like to use a service (factory) that will use the base structure of the class of the PostpaidProduct class to create the structure of the corresponding bundle mapping, so I think I will need this as a mapped superclass.

In my opinion, the way to use it will be to have two separate tables: one for PostpaidProduct and one for PrepaidProduct , as well as one-page inheritance for those for MobilePostpaidProduct / PostpaidProduct and MobilePrepaidProduct / PrepaidProduct .

What do you guys think? Any thoughts on the best way to model this?

+4
source share
1 answer

If you use the RDBMS [MySQL, Postgre] level, I think your suggestion is the best choice, in my opinion.

If the subclasses have * a little more attributes, perhaps the relationship is most related, you are actually advertising the composition and your presentation of the data will not be sparse (that is, you will not have many empty fields in the main table].

On the other hand, if you only want to stick to composition [which is more appropriate in most cases] is $device only additional relation in Mobile classes? If so, you can write the findAllMobileProducts method in your repository class that will return every product where the device is not null, etc.

0
source

Source: https://habr.com/ru/post/1436560/


All Articles