Article.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. /**
  11. * 文章模型
  12. * Class Article
  13. * @package app\common\model
  14. */
  15. class Article extends BaseModel
  16. {
  17. protected $name = 'article';
  18. protected $append = ['show_views'];
  19. /**
  20. * 关联文章封面图
  21. * @return \think\model\relation\HasOne
  22. */
  23. public function image()
  24. {
  25. return $this->hasOne('uploadFile', 'file_id', 'image_id');
  26. }
  27. /**
  28. * 关联文章分类表
  29. * @return \think\model\relation\BelongsTo
  30. */
  31. public function category()
  32. {
  33. $module = self::getCalledModule() ?: 'common';
  34. return $this->BelongsTo("app\\{$module}\\model\\article\\Category");
  35. }
  36. /**
  37. * 展示的浏览次数
  38. * @param $value
  39. * @param $data
  40. * @return mixed
  41. */
  42. public function getShowViewsAttr($value, $data)
  43. {
  44. return $data['virtual_views'] + $data['actual_views'];
  45. }
  46. /**
  47. * 文章详情
  48. * @param $article_id
  49. * @return null|static
  50. * @throws \think\exception\DbException
  51. */
  52. public static function detail($article_id)
  53. {
  54. return self::get($article_id, ['image', 'category']);
  55. }
  56. }