1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- use cores\BaseModel;
- /**
- * 商品规格关系模型
- * Class GoodsSpecRel
- * @package app\common\model
- */
- class GoodsSpecRel extends BaseModel
- {
- // 定义表名
- protected $name = 'goods_spec_rel';
- // 定义主键
- protected $pk = 'id';
- protected $updateTime = false;
- /**
- * 关联规格组
- * @return \think\model\relation\BelongsTo
- */
- public function spec()
- {
- return $this->belongsTo('Spec');
- }
- /**
- * 关联规格值
- * @return \think\model\relation\BelongsTo
- */
- public function specValue()
- {
- return $this->belongsTo('SpecValue');
- }
- /**
- * 指定商品的规格列表
- * @param int $goodsId 商品ID
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getSpecList(int $goodsId)
- {
- // 获取指定商品的规格值关系记录
- $data = (new static)->with(['spec', 'specValue'])
- ->where('goods_id', '=', $goodsId)
- ->select();
- // 规格组
- $groupData = [];
- foreach ($data as $groupKey => $item) {
- $groupData[$item['spec_id']] = [
- 'spec_id' => $item['spec']['spec_id'],
- 'spec_name' => $item['spec']['spec_name']
- ];
- }
- // 去除索引
- $specList = array_values($groupData);
- // 规格值
- foreach ($specList as $groupKey => &$group) {
- $group['key'] = $groupKey;
- foreach ($data as $valueKey => $item) {
- ($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [
- 'key' => isset($group['valueList']) ? count($group['valueList']) : 0,
- 'groupKey' => $groupKey,
- 'spec_value_id' => $item['specValue']['spec_value_id'],
- 'spec_value' => $item['specValue']['spec_value']
- ];
- }
- }
- return $specList;
- }
- }
|