Comment.php 2.4 KB

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