GoodsImage.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\relation\BelongsTo;
  15. /**
  16. * 商品图片模型
  17. * Class GoodsImage
  18. * @package app\common\model
  19. */
  20. class GoodsImage extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'goods_image';
  24. // 定义主键
  25. protected $pk = 'id';
  26. protected $updateTime = false;
  27. /**
  28. * 关联文件库
  29. * @return BelongsTo
  30. */
  31. public function file(): BelongsTo
  32. {
  33. return $this->belongsTo('UploadFile', 'image_id', 'file_id');
  34. }
  35. /**
  36. * 批量写入商品图片记录
  37. * @param int $goodsId 商品ID
  38. * @param array $imageIds 图片ID集
  39. * @param int|null $storeId 商城ID
  40. * @return array|false
  41. */
  42. public static function increased(int $goodsId, array $imageIds, int $storeId = null)
  43. {
  44. $dataset = [];
  45. foreach ($imageIds as $imageId) {
  46. $dataset[] = [
  47. 'image_id' => $imageId,
  48. 'goods_id' => $goodsId,
  49. 'store_id' => $storeId ?: self::$storeId
  50. ];
  51. }
  52. return (new static)->addAll($dataset);
  53. }
  54. }