SkuSpec.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 模型
  6. */
  7. class SkuSpec extends Model
  8. {
  9. // 表名
  10. protected $name = 'shop_goods_sku_spec';
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. // 追加属性
  17. protected $append = [];
  18. /**
  19. * 获取指定商品的SKU信息
  20. * @param int $goods_id 商品ID
  21. * @return array
  22. */
  23. public static function getGoodsSkuSpec($goods_id)
  24. {
  25. $list = (new self)->field('MIN(`id`) AS `id`, MIN(`goods_id`) AS `goods_id`, `spec_id`')->where('goods_id', $goods_id)
  26. ->with([
  27. 'Spec',
  28. 'SkuValue' => function ($query) use ($goods_id) {
  29. $query->where('goods_id', $goods_id)->field('id,goods_id,spec_id,spec_value_id')->with(['SpecValue']);
  30. }
  31. ])->group('spec_id')->select();
  32. $list = collection($list)->toArray();
  33. return $list;
  34. }
  35. public function SkuValue()
  36. {
  37. return $this->hasMany('SkuSpec', 'spec_id', 'spec_id');
  38. }
  39. public function Spec()
  40. {
  41. return $this->hasOne('Spec', 'id', 'spec_id', [], 'LEFT')->bind(['title' => 'name']);
  42. }
  43. public function SpecValue()
  44. {
  45. return $this->hasOne('SpecValue', 'id', 'spec_value_id', [], 'LEFT')->bind(['title' => 'value']);
  46. }
  47. }