ArticleQuery.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/3/17
  6. * Time: 上午10:19
  7. */
  8. namespace common\models\query;
  9. use common\models\Article;
  10. use yii\db\ActiveQuery;
  11. class ArticleQuery extends ActiveQuery
  12. {
  13. /**
  14. * @param null $db
  15. * @return array|Article[]
  16. */
  17. public function all($db = null)
  18. {
  19. return parent::all($db); // TODO: Change the autogenerated stub
  20. }
  21. /**
  22. * @param null $db
  23. * @return array|null|Article
  24. */
  25. public function one($db = null)
  26. {
  27. return parent::one($db); // TODO: Change the autogenerated stub
  28. }
  29. /**
  30. * 被删除的
  31. * @return $this
  32. */
  33. public function onlyTrashed()
  34. {
  35. return $this->andWhere(['not', ['deleted_at' => null]]);
  36. }
  37. /**
  38. * 未被删除的
  39. * @return $this
  40. */
  41. public function notTrashed()
  42. {
  43. return $this->andWhere(['deleted_at' => null]);
  44. }
  45. /**
  46. * 待审核的
  47. * @return $this
  48. */
  49. public function pending()
  50. {
  51. return $this->andWhere(['status' => Article::STATUS_PENDING]);
  52. }
  53. /**
  54. * 审核通过的
  55. */
  56. public function active()
  57. {
  58. return $this->andWhere(['status' => Article::STATUS_ACTIVE]);
  59. }
  60. /**
  61. * 未删除且审核通过的
  62. * @return $this
  63. */
  64. public function normal()
  65. {
  66. return $this->notTrashed()->active();
  67. }
  68. /**
  69. * 已经发布的
  70. * @return $this
  71. */
  72. public function published()
  73. {
  74. return $this->normal()->andWhere(['<', 'published_at', time()]);
  75. }
  76. /**
  77. * @return $this
  78. */
  79. public function my()
  80. {
  81. return $this->andWhere(['user_id' => \Yii::$app->user->id]);
  82. }
  83. }