[英]How to create nested Many to Many relations in parse
I basically have 2 Parse classes in my Online Shop: Product and Order.我的网上商店基本上有 2 个 Parse 类:产品和订单。 While the user fills his shopping cart, I reflect that in a local array of items (javascript object) with a property for the product (parse class) and another property for the amount that particular product should have in the cart.
当用户填充他的购物车时,我在一个本地项目数组(javascript 对象)中反映了这一点,其中包含产品的属性(解析类)和购物车中特定产品应具有的数量的另一个属性。
If I now save the order to parse (I'm using the self hosted open source version of parse-server), I end up with an order object like this:如果我现在保存要解析的订单(我使用的是 parse-server 的自托管开源版本),我最终会得到一个这样的订单对象:
{
"_id": "8XK6gbZZvE",
"items": [
{
"amount": 3,
"product": {
"__type": "Pointer",
"className": "Product",
"objectId": "VWOxzFui6R"
}
}
],
"total": 1800,
"status": "pending",
...
}
My problem with that is that I couldn't find a way to query for orders where the result already includes the products.我的问题是我找不到查询结果已经包含产品的订单的方法。
The only way I currently can think of is to get rid of the amount so that I don't have to nest the product class inside a JS object.我目前能想到的唯一方法是去掉数量,这样我就不必将产品类嵌套在 JS 对象中。 And then just add a product multiple times to the array if necessary.
然后,如有必要,只需将产品多次添加到数组中。
Something like that:类似的东西:
{
"_id": "6j7l5acSB3",
"items": [
{
"__type": "Pointer",
"className": "Product",
"objectId": "VWOxzFui6R"
}
],
"total": 1210,
"status": "processing",
...
}
Which then allows me to include the items in the order query like this query.include('items');
然后允许我在订单查询中包含项目,就像这样
query.include('items');
But would there be also a solution how I could use a structure like my first one and still be able to include the products in the order query?但是是否还有一个解决方案,我可以如何使用像我的第一个结构这样的结构,并且仍然能够在订单查询中包含产品? It kind of feels "redundant" to have the same product multiple times in an array if the user ordered this product eg 2 or 3 times.
如果用户订购了该产品(例如 2 或 3 次),那么在数组中多次使用相同的产品会让人感觉“多余”。
The way this is done in most ecommerce systems is to have another object that adds data to the relationship between order and product, for example...在大多数电子商务系统中这样做的方式是让另一个对象将数据添加到订单和产品之间的关系中,例如......
// Order
{ lineItems: [ <pointer to line item>, < etc > ],
orderTotal: number,
// other fields: probably a customer-pointer, tax, shipping, etc
};
// Line Item - this is the missing concept in the OP
{ product: <pointer to product>,
unitPrice: number,
quantity: number
};
// Product as you have it
Notice that the unitPrice
is probably redundant with a price attribute kept with product.请注意,
unitPrice
可能是多余的,而 price 属性与 product 保持一致。 The app would copy the product price to this field when the product is added to the order.当产品添加到订单时,应用程序会将产品价格复制到此字段。 This enables a business logic decision abut what to do if the product price changes after the order is created.
如果在创建订单后产品价格发生变化,这将支持业务逻辑决策。
The order query can include('lineItems', 'lineItems.product')
订单查询可以
include('lineItems', 'lineItems.product')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.