database design - Best way to store categories and product type in DB -
i have 3 product types. more later. have product categories. category can't have multiple product type. so, how should keep categories.
1.id | parent_id | name
i'll set product types root categories.
example:
electronic > computer > laptop
electronic's parent id 0 , next categories linked due that.
id | parent_id | product_type | name
i'll electronic product_type , computer's parent_id 0.
which way better?
for categories:
id | name | parent_id
(parent id category tree)
for products:
id | category_id | name | description | other
let's category tree like
computers [id: 1 | parent: 0] |-laptops [id: 2 | parent: 1] |-desktop [id: 3 | parent: 1] |-printers [id: 4 | parent: 1] | |-brand #1 [id: 5 | parent: 4] | |-brand #2 [id: 6 | parent: 4] |-other stuff [id: 7 | parent: 1]
then, if want display products from, example, printers
, recursion getting items printers
category , subcategories.
tl;dr
the first way better
Comments
Post a Comment