Article.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\common\model\content;
  3. use app\common\Enum\StatusEnum;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. class Article extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $table = 'article';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'integer';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. ];
  20. /**
  21. * 获取文章
  22. * @param int $categoryId 分类ID
  23. * @param int $page 页码
  24. * @param int $pageSize 每页条数
  25. * @return \think\Paginator
  26. */
  27. public static function getArticleList($categoryId = 0, $page = 1, $pageSize = 10)
  28. {
  29. // 如果有
  30. $categoryList = self::where('status', StatusEnum::ENABLED)
  31. ->field('id,category_id,title,synopsis,author,image,sort')
  32. ->where(function ($query) use ($categoryId) {
  33. if ($categoryId) {
  34. $query->where('category_id', $categoryId);
  35. }
  36. })
  37. ->order('sort desc,id desc')
  38. ->paginate($pageSize, false, ['page' => $page]);
  39. return $categoryList;
  40. }
  41. /**
  42. * 获取文章详情
  43. * @param int $id 文章ID
  44. * @return array|bool|Model|string|\PDOStatement
  45. */
  46. public static function getArticleInfo($id = 0)
  47. {
  48. $categoryInfo = self::where('id', $id)
  49. ->field('id,category_id,title,synopsis,author,image,sort,content')
  50. ->find();
  51. return $categoryInfo;
  52. }
  53. }