I have four types of products which I would like to map to a Doctrine ORM structure on a MySQL RDBMS. The products are 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 basic class structure of the PostpaidProduct
class to create a structure of the corresponding bundle mapping, so I think I would need this as a mapped super class.
In my opinion the way to go would be to have two separate tables, one for PostpaidProduct
and one for PrepaidProduct
, and have a Single Table Inheritance on those for MobilePostpaidProduct
/ PostpaidProduct
and MobilePrepaidProduct
/ PrepaidProduct
.
What do you guys think? Any thoughts on best way to model this?
If you are using a RDBMS layer [MySQL, Postgre] as I think, your proposal is the best choice in my opinion.
If subclasses have * little more attributes, possibly relationships most of all, you are actually promoting composition and your data representation won't be sparse [ie you won't have many empty fields in the main table].
On the other hand, if you want to stick with composition only [which is more advisable in most cases] - is $device
the only additional relationship in Mobile classes? If so, you could write a findAllMobileProducts
method in your repository class that would return every product where device is not null, and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.