I’m making list of items in categories, problem is that item can be in multiple categories. What is your best practice to store items in categories and how list all items within category and its child categories? I am using Zend Framework and MySQL to solve this issue.
Thanks for your replies.
Sorry for my English 🙂
So, you have a hierarchy in the categories, yes? Is it one level (category and child category) or any number (children can have children, etc)? That will impact what the solution is.
Generally, you’d model something like this with a many-to-many relationship, like:
A record in ‘Item_Category’ means that the listed item is in the listed category. You can then do joins between 2 (or 3) of those tables to list which items are in which categories:
Or which categories an item has:
If there’s a hierarchy in the categories, that could be expressed using a recursive relationship in the category table, such as:
That makes things more complicated because you have to use recursive queries to get all the records for a category and its children. A simpler way, if you have only two levels of categories, it to simply make it a second field on the category table, like: