目录
一:创建表
二:代码逻辑
上一节我们详细讲解了自定义扩展字段的逻辑实现和表的设计,这一节我们以一个具体例子演示下,如何实现一个订单模块的自定义扩展数据。
订单主表:
CREATE TABLE `t_order` (
`order_id` int NOT NULL AUTO_INCREMENT,
`order_no` char(20) NOT NULL AUTO_INCREMENT,
`create_user_id` int NOT NULL DEFAULT '0' COMMENT '创建人',
`create_time` int NOT NULL DEFAULT '0' COMMENT '添加时间',
`update_time` int NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`order_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='订单主表';
#订单扩展表,扩展的自定义字段主要存储在这张表
CREATE TABLE `t_order_extend` (
`id` int NOT NULL AUTO_INCREMENT,
`order_id` int NOT NULL DEFAULT '0' COMMENT '订单ID',
`create_time` int NOT NULL DEFAULT '0' COMMENT '添加时间',
`update_time` int NOT NULL DEFAULT '0' COMMENT '更新时间'
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='订单扩展表';
#订单表格数据,订单一对多的数据主要存储在这个表,比如商品
CREATE TABLE `t_order_list_extend` (
`id` int NOT NULL AUTO_INCREMENT,
`order_id` int NOT NULL DEFAULT '0' COMMENT '订单ID',
`create_time` int NOT NULL DEFAULT '0' COMMENT '添加时间',
`update_time` int NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='订单表格数据扩展表';
创建自定义字段部分逻辑
$count = 1;//定义生成字段数量
//生成字段
for ($i=0; $i<$count; $i++) {
//生成字段名
list($fieldSign,$num) = $this->getFieldSign($request['types']);
$fieldSignList[] = $fieldSign;
}
//这里定义一个计数表,用于存储字段的数量,字段的命名以item1,item2,...的方式
$fieldNum = ExtendFieldNumModel::where('extend_type', $types)->value('extend_num', 1);
$fieldSign = 'item_'.$fieldNum;
$fieldWhere = [['field_sign','=', $fieldSign],['types','=',$types]];
if (ExtendField::where($fieldWhere)->value('field_id')) {
ExtendFieldNumModel::where('extend_type', $types)->inc('extend_num', 1)->update();
$this->getFieldSign($types);
} else {
//字段计数
if ($fieldNum == 1) {
ExtendFieldNumModel::create(['extend_type' => $types,'extend_num' => $fieldNum + 1,
'create_time' => time(),'update_time' => time()]);
} else {
ExtendFieldNumModel::where('extend_type', $types)->update(['extend_num' => $fieldNum + 1,
'update_time' => time()]);
}
}
return [$fieldSign,$fieldNum];
//插入字段到字段表
$data = [
'types' => $request['types'],
'field_name' => $request['field_name'],
'field_type' => $request['field_type'],
'default_value' => $request['default_value'] ?? '',
'is_unique' => $request['is_unique'] ?? 2,
'is_require' => $request['is_require'] ?? 2,
'create_time' => time(),
'update_time' => time()
];
$data['field_sign'] = count($fieldSignList) == 1 ? $fieldSignList[0] : implode(',', $fieldSignList);
$fieldId = (new ExtendField())->insertGetId($data);
//扩展表字段部分代码
case 'text':
$sql[] = "ALTER TABLE `" . $table . "` ADD `" . $fieldSign[0] . "` VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '" . $fieldName . "'";
break;
case 'textarea':
$sql[] = "ALTER TABLE `" . $table . "` ADD `" . $fieldSign[0] . "` TEXT COMMENT '" . $fieldName . "'";
break;
if ($sql) {
foreach ($sql as $sValue) {
$resData = Db::execute($sValue);
if ($resData === false) {
return false;
}
}
return true;
}
这样我们就实现了,自定义订单模块的数据了,