CategoryFacilities.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:28
  7. */
  8. namespace common\models;
  9. use yii\db\ActiveRecord;
  10. //use yii\behaviors\TimestampBehavior;
  11. class CategoryFacilities extends Common
  12. {
  13. // public $setDel = 1;
  14. // public function behaviors()
  15. // {
  16. // return [
  17. // [
  18. // 'class' => TimestampBehavior::className(),
  19. // 'attributes' => [
  20. // # 创建之前
  21. // ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
  22. // # 修改之前
  23. // ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
  24. // ],
  25. // #设置默认值
  26. // 'value' => $_SERVER['REQUEST_TIME']
  27. // ]
  28. // ];
  29. // }
  30. public function rules()
  31. {
  32. return [
  33. ['name','required','message'=>'不能为空'],
  34. ['img','string','max'=>40],
  35. ['name','string','min'=>2,'max'=>20,'message'=>'请输入正确的城市名称'],
  36. ['name', 'unique', 'targetClass' => 'common\models\CategoryFacilities','on'=>['add'],'message'=>'名称已经存在'],
  37. ];
  38. }
  39. /*
  40. * 调用里面的验证,错误返回数组,正确返回对象
  41. * */
  42. public function Authenticator($input)
  43. {
  44. $this->load($input,'');
  45. if(!$this->validate()) return $this->errors;
  46. return $this;
  47. }
  48. public function FindById($id)
  49. {
  50. return self::findOne($id);
  51. }
  52. public function getList($page,$arr = [])
  53. {
  54. $query = self::find();
  55. $query->select($arr);
  56. $query = $this->WhereColumn($query);
  57. if(!empty($page['page']))
  58. {
  59. $query->offset = ($page['page'] - 1) * $page['limit'];
  60. $query->limit = $page['limit'];
  61. }
  62. return $query->orderBy(['sort'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
  63. }
  64. private function WhereColumn($query)
  65. {
  66. $query->andFilterWhere(['name'=>$this->name]);
  67. $query->andWhere(['del'=>$this->setDel]);
  68. return $query;
  69. }
  70. public function Total()
  71. {
  72. $query = self::find();
  73. $query = $this->WhereColumn($query);
  74. return $query->count();
  75. }
  76. }