Article.php 1.1 KB

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