SysmenuQ.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/3
  6. * Time: 下午2:24
  7. */
  8. namespace common\models;
  9. //use yii\behaviors\TimestampBehavior;
  10. //use yii\db\ActiveRecord;
  11. class SysmenuQ 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. ['name', 'unique', 'targetClass' => 'common\models\SysmenuQ','on'=>['add']],
  35. ['sort','default','value'=>0],
  36. ['url','string','max'=>50],
  37. ['sort','number'],
  38. ['status','in', 'range' => [1, 2]],
  39. ];
  40. }
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'name'=>'菜单名称',
  45. 'sort'=>'排序',
  46. 'url'=>'地址'
  47. ];
  48. }
  49. public function Authenticator($input)
  50. {
  51. $this->load($input,'');
  52. if(!$this->validate()) return $this->errors;
  53. return $this;
  54. }
  55. public function FindById($id)
  56. {
  57. return self::findOne($id);
  58. }
  59. public function OneFind()
  60. {
  61. $query = self::find();
  62. $query = $this->ManyWhere($query);
  63. return $query->one();
  64. }
  65. public function MultipleConditionQuery($select = null)
  66. {
  67. $query = self::find()->select($select);
  68. $query = $this->ManyWhere($query);
  69. return $query->orderBy(['sort'=>SORT_DESC])->asArray()->all();
  70. }
  71. private function ManyWhere($query)
  72. {
  73. $query->andFilterWhere(['id'=>$this->id]);
  74. $query->andFilterWhere(['del'=>$this->setDel]);
  75. return $query;
  76. }
  77. public function getList($data)
  78. {
  79. $query = $this->QueryFind();
  80. $query = $this->ManyWhere($query);
  81. $query->orderBy('create_at');
  82. if(!empty($data['page']))
  83. {
  84. $query->offset = ($data['page'] - 1) * $data['limit'];
  85. $query->limit = $data['limit'];
  86. }
  87. return $query->orderBy(['sort'=>SORT_DESC])->asArray()->all();;
  88. }
  89. public function Total()
  90. {
  91. $query = $this->QueryFind();
  92. $query = $this->ManyWhere($query);
  93. return $query->count();
  94. }
  95. private function QueryFind()
  96. {
  97. return self::find();
  98. }
  99. }