Comment.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. use think\model\relation\HasMany;
  16. /**
  17. * 商品评价模型
  18. * Class Comment
  19. * @package app\common\model
  20. */
  21. class Comment extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'comment';
  25. // 定义主键
  26. protected $pk = 'comment_id';
  27. /**
  28. * 所属订单
  29. * @return BelongsTo
  30. */
  31. public function orderData(): BelongsTo
  32. {
  33. return $this->belongsTo('Order');
  34. }
  35. /**
  36. * 订单商品
  37. * @return BelongsTo
  38. */
  39. public function orderGoods(): BelongsTo
  40. {
  41. return $this->belongsTo('OrderGoods')
  42. ->field(['order_goods_id', 'goods_id', 'goods_name', 'image_id', 'goods_props', 'order_id']);
  43. }
  44. /**
  45. * 关联用户表
  46. * @return BelongsTo
  47. */
  48. public function user(): BelongsTo
  49. {
  50. return $this->belongsTo('User')->field(['user_id', 'nick_name', 'avatar_id']);
  51. }
  52. /**
  53. * 关联评价图片表
  54. * @return HasMany
  55. */
  56. public function images(): HasMany
  57. {
  58. return $this->hasMany('CommentImage')->order(['id']);
  59. }
  60. /**
  61. * 详情记录
  62. * @param int $commentId
  63. * @param array $with
  64. * @return static|array|null
  65. */
  66. public static function detail(int $commentId, array $with = [])
  67. {
  68. return static::get($commentId, $with);
  69. }
  70. /**
  71. * 添加评论图片
  72. * @param array $images
  73. * @return bool|false
  74. */
  75. protected function addCommentImages(array $images): bool
  76. {
  77. $data = array_map(function ($imageId) {
  78. return [
  79. 'image_id' => $imageId,
  80. 'store_id' => self::$storeId
  81. ];
  82. }, $images);
  83. return $this->image()->saveAll($data) !== false;
  84. }
  85. }