Database design for products and product variants
I'm building an commerce site and I've been researching different ways to show products with variants. I'm trying to do this for contact lenses and wondering what the best way is.
Products can be either simple, such as a lens case or lens solution that have no variants, or in the case of a contact lens, each lens can have up to three configurable variants, depending on the lens, for the prescription (e.g. Power, Base Curve, Diameter).
In my structure I have simple products as just a product, with no parent. A contact lens will be stored as a product, and each variant will be stored as a separate product referencing its parent, and the variants themselves will be linked using a products_variants table.
I have a structure like:
products
- id
- parent_id
- name
- price
- type_id
- brand_id
type
- id
- name
brand
- id
- name
product_variant_group
- id
- name
product_variant_value
- id
- product_variant_group_id
- value
products_variants (isCrossRef = true for Propel relationships)
- id
- product_id
- product_variant_value_id
My code is quite slow already, and I haven't even started to build the select boxes to select the prescription.
public function getPossibleVariants($productId)
{
$product = ProductQuery::create()->findPk($productId);
$children = $product->getChildren();
$options = array();
foreach ($children as $child) {
if ($child->countProductsVariantss()) {
$variants = $child->getProductVariantValues();
foreac开发者_如何转开发h ($variants as $variant) {
$group = $variant->getProductVariantGroup();
$options[$group->getName()][] = $variant->getValue();
}
}
}
ksort($options);
return $options;
}
I'm using propel and Zend Framework. Am I going about this the right way? Most people will need two of the same lens types, but with different prescriptions for each eye. How would I implement this?
I've tried looking at other systems, such as magento and oscommerce. The former confuses me and I find the code messy in the latter. As this is a learning experience, I'd like to do things the best possible way.
Thanks
It is better to use one table for all your products and products variants. So that the operations based on products get easier. Ultimately a variant is a product only.
So I would suggest.
products
- id
- parent_id
- name
- price
- type_id
- brand_id
- variant_id
The additional field variant_id
should reference the variant group.
It is simpler this way. And you can retrieve all the variants based on variant group id.
I have played with Amazon product API a lot. Their approach is similar to this.
精彩评论