Database design 基于laravel 4的电子商务应用程序的关系数据库设计建议
我用Laravel 4开发了一个电子商务应用程序,需要一个建议来解决我的数据库设计问题 问题:Database design 基于laravel 4的电子商务应用程序的关系数据库设计建议,database-design,orm,laravel,eloquent,has-and-belongs-to-many,Database Design,Orm,Laravel,Eloquent,Has And Belongs To Many,我用Laravel 4开发了一个电子商务应用程序,需要一个建议来解决我的数据库设计问题 问题: <?php class Products extends Eloquent { protected $table = 'products'; protected $fillable = array('name'); public function variations() { return $this->belongsToMany('Variations','pro
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
我的客户的产品有多种变体/选项。例如:
产品1=苹果Iphone 5
颜色选项:黑色
黑色选项1:16GB 股票期权1:15 期权1的售价:900美元
颜色选项:白色
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
正如你在上面看到的,有很多产品有不同类型的选择,股票,价格。我用belongtomany()尝试了以下方法:
表格:产品
id
name
created_at
updated_at
id
title
parent
created_at
updated_at
表格:变化
id
name
created_at
updated_at
id
title
parent
created_at
updated_at
表格:产品变化
id
products_id
variations_id
stock
purchase_price
sales_price
created_at
updated_at
产品型号:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
每个具有多个值的属性都应该有自己的表,其中外键是基本项——例如,可以将iPhone放在基本表中,颜色、内存大小等作为它们自己的表。然后,定价表包含价格和所有完整描述商品的外键(白色64GB iPhone)
在进行“数据库规范化”时,您可能需要查找它。选项表应该是HASANDBELONGTOMANY(),并带有Products表。
然后,每种股票和价格都应该在期权表中有ANDBELONGTOMANY()
产品表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
选项表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
选项\u产品表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
库存表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
期权\u股票表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
价格表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
选项\u价格表:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
id
product_name
created_at
updated_at
id
option_name
created_at
updated_at
id (optional(
option_id
product_id
created_at
updated_at
id
count
created_at
updated_at
id (optional)
option_id
stock_id
created_at
updated_at
id
price
created_at
updated_at
id (optional)
option_id
price_id
created_at
updated_at
这将使您能够有一个选项颜色紫色,可以应用于许多不同的产品,许多不同的紫色价格的条目。
此外,您的查询也不会太糟糕,因为您可以急切地加载选项和其他表(请参阅)
我建议您看看noSQL数据库,比如。
就我而言,我在一些项目中的一些产品/服务也有类似的问题。Json/对象存储帮助了我,我真的很高兴我决定搬到couchbase。我猜有些产品有变化,有些没有变化。在某个时候,您将希望为产品(和变体)创建订单。如果产品和变体被建模为完全不同的实体,那么建模订单和其他事情将变得很麻烦。我建议一个更简单的解决方案,其中产品和变体都被建模为产品,变体或选项通过关联表链接到父产品,如下所示:
产品
- 产品标识(pk)
- 名字
- 价格
产品选项图
- 父项产品标识(fk到产品。产品标识)
- 选项\产品\标识(fk到product.product\标识)
- 选项编号(如有必要确保订购)
如有必要,您可以在产品表中添加一个“类型”列,这将允许您的系统轻松识别具有选项的产品。当然,这总是可以从父产品标识与给定产品标识匹配的产品选项映射行中推断出来
我注意到另一位受访者在自己的表格中对价格进行了建模。我鼓励您与您的客户讨论定价要求,因为现实世界中的系统很难只存储每个产品的单一价格。你可能会发现有各种各样的促销活动、批量折扣、优惠券、预先安排的客户折扣计划等等。。。在产品表中存储“售价”很可能是不够的。看看我的迁移文件,我已经用产品的属性和属性分开了
- 属性为正面,或者客户可以更改属性(例如:颜色、大小)
- 属性是产品的固定属性,例如:床单材料(例如:棉)
为什么不将每个产品变体作为自己的产品?i、 产品1是Iphone5白色,产品2是Iphone黑色等等,这是非常老式的方式。因为这家店真的很难管理。前端和后端。让我们想象一下,作为一家T恤店,您是一位想要购买阿迪达斯345 thirt黑色XL码T恤的客户。导航,找到产品就像一场噩梦…看到了吗?Thaks这对于这个问题非常有用。仍然在寻找关于你建议的有说服力的关系。顺便说一句,在你的结构中,变化没有自己的股票和价格。嗯,很好的建议,非常感谢。但是我在文档hasandbelongtomany()中没有看到。你是说belongtomany()?是的,belongtomany()就是你想要的。hasandbelongtomany()是一个试图插入的旧Laravel 3。@dr.linux您找到了一个解决方案吗