简体   繁体   English

CakePHP是否适合我的项目(小型商店)中的CakePHP数据库结构和模型关系?

[英]The right database structure and model relations in CakePHP for my project (small shop)?

I have a pretty simple shop-system. 我有一个非常简单的商店系统。 I'm working with CakePHP. 我正在使用CakePHP。 Actually I wouldn't call it shop, it's rather a basic form where you can type in your data and which items in which color you want and that's it. 实际上,我不会将其称为“商店”,它是一种基本形式,您可以在其中键入数据以及所需颜色的项目,仅此而已。 There is one buying-form which is "open to the public" and then there are buying-forms which are password secured. 有一种“向公众开放”的购买表格,然后有受密码保护的购买表格。 The latter ones have a selection of the items (or selection of colors) which you get on the public site, but have discounts. 后者选择了您在公共网站上获得的商品(或颜色),但有折扣。 I want to save the orders in a database. 我想将订单保存在数据库中。 I have a table orders and ordered_products. 我有一张桌子订单和ordered_products。 That's working fine. 很好 It works pretty good, but only because I made something not very good: Since there are just a few products I just wrote an array in the controller with the names, prices and stuff... the discounts or selection of products I handled by just overwriting the products-property. 它工作得很好,但这只是因为我做了一些不好的事情:由于只有很少的产品,我只是在控制器中写了一个数组,上面写着名称,价格和东西……我所处理的产品的折扣或选择覆盖产品属性。

Well, putting data in the controller is not really the idea behind the MVC-Structure, so I was thinking about who to handle the products, the selection of products for the different password-secured buying forms and the discounts with models. 嗯,将数据放入控制器并不是MVC结构背后的真正想法,所以我在考虑由谁来处理产品,为具有密码保护的不同购买形式选择产品以及模型的折扣。

My idea was, to create the following tables: 我的想法是创建以下表格:

products (id, name, price,...) -hasAndBelongsToMany Color
colors (id, name)
products_colors (product_id, color_id)

Now to set in which "closed-area", which products in which color and with which special price can be ordered I thought of the following tables (the actual table and field names are of course not wise chosen, but just for the idea): 现在设置哪个“封闭区域”,可以订购哪种颜色和特价的产品,我想到了以下表格(实际的表格和字段名称当然不是明智的选择,而只是出于想法) :

product_selections (id, closed-area_name, product_id, special_price) hasAndBelongsToMany Color
product_selections_colors (product_selection_id, color_id)

When I'm creating the public buying form I would just use the top three tables. 在创建公共购买表单时,我只会使用前三个表格。 but building the "closed-area" I would use the bottom two, selecting the product_ids and special_prices from product_selection as well as the different colors over the product_selections_colors-table for the according "closed-area" (i dont know a better name for that right now...). 但要建立“封闭区域”,我将使用底部两个区域,从product_selection中选择product_ids和special_prices以及product_selections_colors表上对应于“封闭区域”的不同颜色(我不知道该名称的更好名称)马上...)。 with the product_id i would get the other information about the product from the table products and create the buying form with this data. 使用product_id,我将从表产品中获取有关产品的其他信息,并使用此数据创建购买表单。

I want to have it all in a database, because then I can better work with the orders (creating receipts, deliverynotes etc.). 我希望将其全部存储在数据库中,因为这样我可以更好地处理订单(创建收据,交货单等)。

I would like to know what you think about that, because I have the feeling that I'm going totally in the wrong direction, since it feel way to complicated for such a simple thing... I hope you can help me. 我想知道您对此有何想法,因为我感觉自己完全朝错误的方向前进,因为这样一件简单的事情会让我感到复杂……我希望您能为我提供帮助。

Based on your description, I would recommend doing it this way: 根据您的描述,我建议这样做:

Have a users table with a field for "group_id". 有一个用户表,其中包含“ group_id”字段。 This allows you to have multiple users with login privileges that all can view the same options or colors based on their grouping. 这使您可以拥有多个具有登录特权的用户,所有用户都可以根据其分组查看相同的选项或颜色。

In the case of a general (non-logged in) user, the assign the group_id to default to 0. 对于普通用户(未登录),请将group_id分配为默认值0。

Next, ditch the product_selections and product_selections_colors tables. 接下来,放弃product_selections和product_selections_colors表。 You don't want to have to repeat products across tables. 您不需要在表之间重复产品。 Simply add a new table that pairs which product ids can be purchased by which group_ids. 只需添加一个新表,即可将哪些group_ids可以购买的产品id配对。 (HABTM relationship in cake) (蛋糕中的HABTM关系)

You will obviously need to tweak this general setup to work specifically for your needs. 显然,您将需要调整此常规设置以专门满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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