简体   繁体   中英

Food item options database design

Am building a website where restaurants can put their menus online for people to order from. Am having issues with my database design, i've been to set up my tables like this

*/----------------*
* menus
*-----------------*
id
restaurant_id
name
active
created_at
updated_at

*/----------------*
* menu_categories
*-----------------*
id
menu_id
name
created_at
updated_at

*/----------------*
* items
*-----------------*
id
menu_id
menu_category_id
name
price
description
active
created_at
updated_at

However i cant figure out how to store options(variations) for a menu items. For example a menu item that served with different kinds of sauces and meats, with the meats only affecting the price of the item. What would a good db table look like for this. I appreciate any help, Thanks.

I would create a table called item_variations that would look like

id
item_id
name
variation_type
price_variation
active
created_at
updated_at

You could then have one or many entries in this table that are linked to the item using item_id . The table would contain the variation name, type and price difference. A typical record would look something like

id: 1
item_id: 2
name: Lamb
variation_type: Meat
price_variation: 1.00
active: 1
....

Or

id: 2
item_id: 2
name: Mint sauce
variation_type: Sauce
price_variation: 0.50
active: 1
....

Converting my comment to an answer: I'd say you need an options table and a items_options table, so you can relate many options with many items. IE: chicken_breast -> bbq_sauce , chicken_breast -> honey_mustard , sirloin -> bbq_sauce

*/----------------* * options *-----------------* id name price? ...

*/----------------* * items_options *-----------------* id_item id_option price? ...

price could be part of any of the two tables, depending on your implementation (if you want options to always cost the same, or cost differently depending on the item)

You should create another table called options_(menuid)
This table should be created when a new menu is created

*/----------------*
* options_(menuid)
*-----------------*
id
menu_category_id
menu_item_id
name
price
description
active
created_at
updated_at

Basically, your script should create a new table for each menu in order to keep clustering down since there will probably be a lot of options per menu. menu_category_id is the ID of the category, and menu_item_id will be the ID of the menu item.

Some ideas on querying:

 -List Menus (SELECT * FROM menus WHERE restaurant_id='$restaurant' AND active=true)
   -List Categories (SELECT * FROM menu_categories WHERE menu_id='$menu')
     -List Menu Items (SELECT * FROM items WHERE menu_id='$menu' AND menu_category_id='$menu_category' AND active=true)
       -List item options (SELECT * FROM options_id WHERE menu_category_id='$menu_category' AND menu_item_id='$item' AND active=true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM