Characteristic.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:30
  7. */
  8. namespace common\models;
  9. //use yii\db\ActiveRecord;
  10. //use yii\behaviors\TimestampBehavior;
  11. class Characteristic extends Common
  12. {
  13. public function rules()
  14. {
  15. return [
  16. ['name','required','message'=>'不能为空'],
  17. ['name', 'unique', 'targetClass' => 'common\models\Characteristic','on'=>['add'],'message'=>'该特色主题已存在','filter'=>function($query){
  18. return $query->andWhere(['del'=>$this->setDel]);
  19. }],
  20. // ['name', 'unique', 'targetClass' => 'common\models\Characteristic','message'=>'该名称已经存在'],
  21. ['name','string','max'=>50,'message'=>'请输入最大100位的值'],
  22. [['del','is_show'],'in','range'=>[1,2]],
  23. ];
  24. }
  25. public function getList($input,$select = null)
  26. {
  27. $query = $this->FindQuery();
  28. $query->select($select);
  29. $query = $this->WhereFocus($query);
  30. if(!empty($input['page']))
  31. {
  32. $query->limit = $input['limit'];
  33. $query->offset = ($input['page']-1 )* $input['limit'];
  34. }
  35. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  36. }
  37. public function getAllList($input,$select = null)
  38. {
  39. $query = $this->FindQuery();
  40. $query->select($select);
  41. $query->andFilterWhere(['name'=>$this->name]);
  42. $query->andWhere(['del'=>$this->setDel]);
  43. $query->andFilterWhere(['id'=>$this->id]);
  44. if(!empty($input['page']))
  45. {
  46. $query->limit = $input['limit'];
  47. $query->offset = ($input['page']-1 )* $input['limit'];
  48. }
  49. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  50. }
  51. public function Total()
  52. {
  53. $query = $this->FindQuery();
  54. return $this->WhereFocus($query)->count();
  55. }
  56. // public function ManyId()
  57. // {
  58. // $query = $this->FindQuery();
  59. // $query = $this->WhereFocus($query);
  60. // return $query->asArray()->all();
  61. // }
  62. private function WhereFocus($query)
  63. {
  64. $query->andFilterWhere(['name'=>$this->name]);
  65. $query->andWhere(['del'=>$this->setDel]);
  66. $query->andWhere(['is_show'=>1]);
  67. $query->andFilterWhere(['id'=>$this->id]);
  68. return $query;
  69. }
  70. private function FindQuery()
  71. {
  72. return self::find();
  73. }
  74. public function Authenticator($input)
  75. {
  76. $this->load($input,'');
  77. if(!$this->validate()) return $this->errors;
  78. return $this;
  79. }
  80. public function FindById($id)
  81. {
  82. return self::findOne($id);
  83. }
  84. public function getListAll()
  85. {
  86. return self::find()->select(['id','name'])->andWhere(['del'=>1,'is_show'=>1])->asArray()->all();
  87. }
  88. }