商品模型是商品添加前的一个重要步骤,定义好商品模型具备哪些规格,这些规格具备哪些规格值,同时商品模型对应哪些属性,将这些数据入库保存后,在添加商品时:
可以选择要添加的商品属于哪种模型,随即会组装该商品对应的规格以及规格值,也就是SKU数据。
在添加商品模型数据时,会接收到:模型名称、规格名集合、规格值集合、属性集合,用户在添加时可能由于误操作导致规格名为空、规格名对应的一组规格值为空、个别规格值为空、属性名为空等等现象,需要在添加之前做过滤。数据添加一部分是单条数据入库,一部分是批量保存入库,所以更涉及到数组的转换。
数据库:类型表
属性表
规格表
规格值表
路由:
//列表
Route::get('index', 'Type/index');
//添加
Route::get('save', 'Type/save');
控制器:
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use think\Exception;
use think\facade\Db;
use think\Request;
class Type
{
/**
* 显示资源列表
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index($id)
{
$info = (new \app\admin\model\Type())->with(['attr','spec','spec.specvalue'])->find($id)->toArray();
return success(200,'ok',$info);
}
/**
* 添加
*
* @throws Exception
*/
public function save()
{
$data = input();
//类型
\app\admin\business\Type::check($data);
return success(200,'ok','添加成功');
}
}
业务层:
<?php
namespace app\admin\business;
use app\admin\model\Attribute;
use app\admin\model\Spec;
use app\admin\model\SpecValue;
use think\Exception;
use think\facade\Db;
class Type
{
/**
* @param $data
* @throws Exception
*/
public static function check($data){
Db::startTrans();
try {
//类型表
$type = \app\admin\model\Type::create([
'type_name' => $data['type_name'],
'create_time' => time()
]);
$type_id = $type->id;
//属性表
$attrModel = new Attribute();
$attrAdd = [];
foreach($data['attr'] as $item){
$attrAdd[] = [
'attr_name' => $item['attr_name'],
'type_id' => $type_id,
'attr_values' => $item['attr_values'],
'sort' => $item['sort'],
'create_time' => time()
];
}
//存入数据库
$attrModel->saveAll($attrAdd);
//规格表
$specModel = new Spec();
$specAdd = [];
foreach($data['spec'] as $val){
$specAdd[] = [
'spec_name' => $val['spec_name'],
'type_id' => $type_id,
'sort' => $val['sort'],
'create_time' => time()
];
}
//存入数据库
$specResult = $specModel->saveAll($specAdd);
//spec_value
//存入规格表的数据
$specInfo = $specResult->toArray();
// dd($specInfo);
//array_column() 返回输入数组中某个单一列的值。
$specInfo = array_column($specInfo,'id','spec_name');
// dd($specInfo);
//规格值
$specvalueAdd = [];
foreach($data['spec'] as $val){
foreach ($val['specvalue'] as $value){
$specvalueAdd[] = [
'spec_id' => $specInfo[$val['spec_name']],
'spec_value' => $value,
'type_id' => $type_id,
'create_time' => time()
];
}
}
$specValueModel = new SpecValue();
$specValueModel->saveAll($specvalueAdd);
//提交事务
Db::commit();
}catch(\Exception $e){
//回滚事务
Db::rollback();
throw new Exception($e->getMessage(),$e->getCode());
}
}
}
这个是上面index列表展示模型关联
<?php
declare (strict_types = 1);
namespace app\admin\model;
use think\Model;
/**
* @mixin \think\Model
*/
class Type extends Model
{
/**
* 类型与属性的关系
*
* @return \think\model\relation\HasMany
*/
public function attr(){
return $this->hasMany(Attribute::class,'type_id','id');
}
/**
*类型与规格名的关系
*
* @return \think\model\relation\HasMany
*/
public function spec(){
return $this->hasMany(Spec::class,'type_id','id');
}
}
添加信息