Category modeling logic in an e-commerce

Asked

Viewed 209 times

1

I am with a project to create an e-commerce, in which my doubt refers to the modeling of tables:

Category(ROOT)>sub-categories>sub-categories of sub-category.

As should be these tables, and a product may be within categories pharisaic (root), and in them may also be in sub-categories. The tables would have to be able to work the same as the print below, follows: Exemplo de menu existente em um e-commerce, assim que terei que fazer

Below is an example of what I did: inserir a descrição da imagem aqui

1 answer

1


Categories with Sub-Categories, I usually make a single table. In this table I add a field idCategoriaPai.

When making a category registration I leave this field null. When I register a sub-category, I fill this field with the id parent-category.

If this field is filled in, the category is a sub-category. If it is not, the category is the root category.

If you need to return the name of all parent categories in your sub-category just make a recursive function.

This way, you can do several levels of sub-categories.

I don’t know if it’s the best way, but it’s the way I usually work.

  • would say the same thing :)

  • Roberto, I edited my question and added another image, being it reference of your example, would be this?

  • In my view you don’t even need this table categorias_has_produtos. The field Categorias_id can be directly on the table Produtos. That’s because, the category and product ratio is 1-1, if it were 1-N ai yes this third table would be necessary.

  • but this is the case, the product may be in more than one category at the same time, hence it would be necessary to table categorias_has_products, correct?

  • Ah yes, so it is necessary yes this third table.

  • @Robertofagundes, thank you for your help!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.