[英]Laravel Model Relationships
I am playing around with Laravel Models and i came across a fairly common database design pattern for which i am unable to establish relationships (using Laravel). 我在玩Laravel模型,遇到了一个相当普通的数据库设计模式,无法建立关系(使用Laravel)。
Here is the Database design
这是
Database design
If we only take into consideration the following tables (for simplicity): 如果仅考虑下表(为简单起见):
1.Customers 1.客户
2.Products 2.产品
3.Product_Prices (The products price is expected to change over time and we need to keep track) 3.Product_Prices(产品价格预计会随时间变化,我们需要保持跟踪)
4.Customer_Orders 4.Customer_Orders
5.Customer_Orders_Products (A customer can order multiple products in a single order) 5.Customer_Orders_Products(客户可以在一个订单中订购多个产品)
Here is what i have so far: 这是我到目前为止所拥有的:
1. Product HAS MANY
Product_Prices 1.产品
HAS MANY
Product_Prices
2. Product_Prices BELONGS TO
Products 2. Product_Price
BELONGS TO
产品
3. Customer HAS MANY
Customer_Orders 3.客户
HAS MANY
Customer_Orders
4. Customer_Order BELONGS TO
Customer 4. Customer_Order
BELONGS TO
客户
5. Customer_Order HAS MANY
Customer_Order_Products 5. CUSTOMER_ORDER
HAS MANY
Customer_Order_Products
6. Customer_Order_Products BELONGS TO
Customer_Order 6. Customer_Order_Products
BELONGS TO
Customer_Order的影响
I think that the Price
for each Customer_Order_Products
will be fetched by establishing a relationship to the Products
table? 我认为将通过与
Products
表建立关系来获取每个Customer_Order_Products
的Price
? How do we establish such a relationship? 我们如何建立这种关系?
How to get an Laravel Collection Object
with the following details (I am not sure if only Eloquent
returns a collection object or if its the same with Fluent
): 如何获取具有以下详细信息的
Laravel Collection Object
(我不确定是否只有Eloquent
返回一个集合对象,或者与Fluent
相同):
1. Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WITH Product_Price
? 1.
Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WITH Product_Price
?
Also how to put constrains like: 还有如何施加约束,例如:
1. Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WITH Product_Price
WHERE Product_Price < Customers_Order.date_order_placed
? 1.
Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WITH Product_Price
WHERE Product_Price < Customers_Order.date_order_placed
?
Thanks 谢谢
I think de-normalizing the database to include the price in Customer_Order_Products and then use following relationship would be better idea. 我认为取消规范化数据库以将价格包含在Customer_Order_Products中,然后使用以下关系将是更好的主意。
class CustomerOrder {
protected $table = 'Customer_Orders';
...
public function products()
{
return $this->belongsToMany('Products')->withPivot('quantity','comments', 'price');
}
...
}
In your case the Customer_Orders_Products isn't just a fancy join table, but a model of it's own with a relationship to the Product_Prices table. 在您的情况下,Customer_Orders_Products不仅是精美的联接表,而且还具有与Product_Prices表之间的关系。
Add a product_price_id to the table to map the ordered product to the price. 将product_price_id添加到表中,以将订购的产品映射到价格。
This adds flexibility (maybe the price is determined by volume later instead of 'recentness') at almost the same overhead cost as storing the price directly. 这增加了灵活性(也许价格是由以后的价格而不是“近期性”决定的),其间接费用几乎与直接存储价格相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.