CategoryNews.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:29
  7. */
  8. namespace common\models;
  9. //
  10. //use yii\db\ActiveRecord;
  11. //use yii\behaviors\TimestampBehavior;
  12. class CategoryNews extends Common
  13. {
  14. // public $dels = 1; //废弃
  15. // public $item = [1=>'不推荐',2=>'推荐'];
  16. // public function behaviors()
  17. // {
  18. // return [
  19. // [
  20. // 'class' => TimestampBehavior::className(),
  21. // 'attributes' => [
  22. // # 创建之前
  23. // ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
  24. // # 修改之前
  25. // ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
  26. // ],
  27. // #设置默认值
  28. // 'value' => $_SERVER['REQUEST_TIME']
  29. // ]
  30. // ];
  31. // }
  32. public function FindById($id)
  33. {
  34. return self::findOne($id);
  35. }
  36. public function rules()
  37. {
  38. return [
  39. ['news_name','required','message'=>'不能为空'],
  40. ['sort','number'],
  41. ['state','default','value'=>1],
  42. ['recommend','in','range'=>[1,2]],
  43. ];
  44. }
  45. public function getList($input,$select = null,$state = null)
  46. {
  47. $query = $this->FindQuery();
  48. $query->select($select);
  49. $query = $this->WhereFocus($query);
  50. if (!empty($state) && $state == 1) {
  51. $query->andWhere(['state'=>1]);
  52. }
  53. if (!empty($input['page']))
  54. {
  55. $query->limit = $input['limit'];
  56. $query->offset = ($input['page'] - 1) * $input['limit'];
  57. }
  58. return $query->orderBy(['state'=>SORT_ASC,'sort'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
  59. }
  60. public function WhereFocus($query)
  61. {
  62. $query->andFilterWhere(['like','news_name',$this->news_name]);
  63. $query->andWhere(['del'=>$this->setDel]);
  64. return $query;
  65. }
  66. public function Total()
  67. {
  68. $query = $this->FindQuery();
  69. $query = $this->WhereFocus($query);
  70. return $query->count();
  71. }
  72. private function FindQuery()
  73. {
  74. return self::find();
  75. }
  76. }