First of all, let me make clear that I'm not asking for any code; I just wan't some general ideas/guidance/opinions about how could I implement what I'm about to ask.
I'm starting to build an online e-commerce system (Yii2 + MongoDB, so, PHP + NoSQL), and there are two requisites that I'm not entirely sure how to implement without creating a huge mess in both my code and the database.
Both requisites are related, so I'll explain them both as one.
As any other serious e-commerce, it would have categories. And also, as any other serious e-commerce, each product will have tags
or options
. Let me explain a little bit further what I call tags
/options
.
Those are the available options that a user could select when buying a product, for example, the color or the size, the material, etc.
- Categories
There would be multiple general
categories as well as other sub-categories. For example, Electronics
could be a general category and sub-categories would be Computers
and Smart TVs
. Then, Motherboards
and RAM
could be sub-categories of Computers
.
This by itself could be easily stored in a database, but here it goes the problem:
- Each product should appear when listing any of the categories it belongs to, or the upper categories. That means that if I (as the final user) browse all the items in
Computers
category, I should seeNVIDIA GTX670
which belongs to the subcategoryGraphic cards
of the categoryComputers
.
I could save each product the following way:
{
_id: asdasfwetrw34tw34t245y45y,
name: "NVIDIA GTX670",
price: 99.50,
...
...
categories: [
"Electronics", //<-- just the ID of that group
"Computers", //<-- just the ID of that group
"Graphic cards" //<-- just the ID of that group
]
}
But:
- I'm not sure how fast would be a query for retrieving all the items of a certain category (and, of course, the items of all sub-categories).
- I'm not sure what other cons would that method have, so, please feel free to recommend any alternative schema for storing this.
2. Tags/options
This is where the real headache is.
Each option could belong to 0 or more categories and subcategories, so the category Woman fashion
could have the options size
and color
, but the category Sunglasses
(subcategory of Woman fashion
) could have only color
, or even another set of options, completely different from Woman fashion
.
Furthermore, the values inside each option (red
, green
, blue
in the color
option) could appear in random categories. So Woman fashion
would have colors like Strawberry Red
and Tangerine
, while Cars
would have Carbon
and Black metallic
.
Also, there would be several types of options:
- Completely static (like
size
, which could be onlyS
orM
, but never both. In any case, the administrator won't be able to write a custom size, likeKind of small
; he would be able to just select what it's already in the database). - Static that can combine together (like
colors
, which could bered
orgreen
, or a combination of colors that the admin chooses). - Free-input (like
dimensions
orweight
, that would, ideally, be input fields and dropdown values to join with. For example[10]
|(mg||kg|tons)
or[20]
(cm|m|km|miles)
).
I could save each option like this:
{
option: "Color",
type: "Static with combinations"
values: [
{
value: "Red",
categories: [
"Sunglasses"
]
},
{
value: "Green",
categories: [
"Sunglasses",
"T-Shirts"
]
},
{
value: "Black metallic",
categories: [
"Cars"
]
}
],
categories: [
"Woman fashion", //<-- only the ID of this group
"Cars" //<-- only the ID of this group
]
}
But I'm worried about how big a single option could turn to be, when there are 30 categories and each value of the option is set to appear in random categories.
Also I just don't see it clean enough, but maybe that is just me.
Anyways, as with the previous point, please feel free to suggest anything to can come up with, I'll greatly appreciate any feedback that you can give me.