Article.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\article;
  12. use app\model\product\product\StoreProduct;
  13. use crmeb\traits\ModelTrait;
  14. use crmeb\basic\BaseModel;
  15. use think\Model;
  16. /**
  17. * TODO 文章Model
  18. * Class Article
  19. * @package app\model\article
  20. */
  21. class Article extends BaseModel
  22. {
  23. use ModelTrait;
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'article';
  34. /**
  35. * 商品一对一关联
  36. * @return \think\model\relation\HasOne
  37. */
  38. public function storeInfo()
  39. {
  40. return $this->hasOne(StoreProduct::class, 'id', 'product_id')
  41. ->field('store_name,image,price,id,ot_price');
  42. }
  43. /**
  44. * 文章详情一对一关联
  45. * @return \think\model\relation\HasOne
  46. */
  47. public function content()
  48. {
  49. return $this->hasOne(ArticleContent::class, 'nid', 'id')->bind(['content']);
  50. }
  51. /**
  52. * 文章详情一对一关联
  53. * @return \think\model\relation\HasOne
  54. */
  55. public function cateName()
  56. {
  57. return $this->hasOne(ArticleCategory::class, 'id', 'cid')->bind(['catename'=>'title']);
  58. }
  59. /**
  60. * 文章图片获取器
  61. * @param $value
  62. * @return array|false|string[]
  63. */
  64. protected function getImageInputAttr($value)
  65. {
  66. return explode(',', $value) ?: [];
  67. }
  68. /**
  69. * 文章分类搜索器
  70. * @param Model $query
  71. * @param $value
  72. * @param $data
  73. */
  74. public function searchCidAttr($query, $value, $data)
  75. {
  76. if ($value) {
  77. $query->where('cid', $value);
  78. }
  79. }
  80. /**
  81. * 文章标题搜索器
  82. * @param Model $query
  83. * @param $value
  84. * @param $data
  85. */
  86. public function searchTitleAttr($query, $value, $data)
  87. {
  88. $query->where('title', 'like', '%' . $value . '%');
  89. }
  90. /**
  91. * 热门文章搜索器
  92. * @param Model $query
  93. * @param $value
  94. * @param $data
  95. */
  96. public function searchIsHotAttr($query, $value, $data)
  97. {
  98. $query->where('is_hot', $value);
  99. }
  100. /**
  101. * 轮播文章搜索器
  102. * @param Model $query
  103. * @param $value
  104. * @param $data
  105. */
  106. public function searchIsBannerAttr($query, $value, $data)
  107. {
  108. $query->where('is_banner', $value);
  109. }
  110. }