ArticleCategory.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\models\article;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * TODO 文章分类Model
  7. * Class ArticleCategory
  8. * @package app\models\article
  9. */
  10. class ArticleCategory extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'article_category';
  22. use ModelTrait;
  23. /**
  24. * TODO 获取文章分类
  25. * @return false|\PDOStatement|string|\think\Collection
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. */
  30. public static function getArticleCategory()
  31. {
  32. return self::where('hidden', 0)->where('is_del', 0)->where('status', 1)->where('pid', 0)->order('sort DESC')->field('id,title')->select();
  33. }
  34. public static function getIdArticleCategoryWithChildren($pid = 0)
  35. {
  36. $categories = self::where('hidden', 0)
  37. ->where('is_del', 0)
  38. ->where('status', 1)
  39. ->where('pid', $pid)
  40. ->order('sort DESC')
  41. ->field('id,title,pid')
  42. ->select();
  43. foreach ($categories as &$category) {
  44. $category['children'] = self::getIdArticleCategoryWithChildren($category['id']);
  45. }
  46. return $categories;
  47. }
  48. /**
  49. * TODO 获取分类字段
  50. * @param $id $id 编号
  51. * @param string $field $field 字段
  52. * @return mixed|string
  53. */
  54. public static function getArticleCategoryField($id, $field = 'title')
  55. {
  56. if (!$id) return '';
  57. return self::where('id', $id)->value($field);
  58. }
  59. /**
  60. * @param $cid
  61. * @param $first
  62. * @param $limit
  63. * @param string $field
  64. * @return \think\Collection
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. */
  69. public static function cidByArticleList($cid, $first, $limit, $field = '*')
  70. {
  71. $model = new Article();
  72. if ($cid) $model->where('cid', $cid);
  73. return $model->field($field)->where('status', 1)->where('hide', 0)->order('sort DESC,add_time DESC')->limit($first, $limit)->select();
  74. }
  75. }