Sysmenu.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //use yii\db
  12. class Sysmenu extends Common
  13. {
  14. public $title;
  15. // public $setDel = 1;
  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 rules()
  33. {
  34. return [
  35. ['name','required','message'=>'不能为空'],
  36. ['name', 'unique', 'targetClass' => 'common\models\Sysmenu','on'=>['add']],
  37. [['pid','sort'],'number'],
  38. ['sort','default','value'=>0],
  39. ['url','string','min'=>1],
  40. ['path','string','max'=>100],
  41. ['icon','string','max'=>50],
  42. ['status','in', 'range' => [1, 2, 3]],
  43. ];
  44. }
  45. public function Authenticator($input)
  46. {
  47. $this->load($input,'');
  48. if(!$this->validate()) return $this->errors;
  49. return $this;
  50. }
  51. public function FindById()
  52. {
  53. return self::findOne($this->id);
  54. }
  55. public function OneFind()
  56. {
  57. $query = self::find();
  58. $query = $this->ManyWhere($query);
  59. return $query->one();
  60. }
  61. public function MultipleConditionQuery($select = null)
  62. {
  63. $query = self::find()->select($select);
  64. $query = $this->ManyWhere($query);
  65. return $query->orderBy(['sort'=>SORT_DESC])->asArray()->all();
  66. }
  67. private function ManyWhere($query)
  68. {
  69. $query->andFilterWhere(['pid'=>$this->pid]);
  70. $query->andFilterWhere(['id'=>$this->id]);
  71. $query->andFilterWhere(['status'=>$this->status]);
  72. // $query->andFilterWhere(['del'=>$this->setDel]);
  73. return $query;
  74. }
  75. public function getList($data)
  76. {
  77. $query = self::find();
  78. $query->andFilterWhere(['status'=>$this->status]);
  79. $count = $query->count();
  80. $query->select(['id','name','icon','url','pid',"CONCAT(path,id,',') as paths",'status','sort']);
  81. $query->orderBy('paths,create_at');
  82. if(!empty($data['page']))
  83. {
  84. $query->offset = ($data['page'] - 1) * $data['limit'];
  85. $query->limit = $data['limit'];
  86. }
  87. $arr['count'] = $count;
  88. $arr['data'] = $query->asArray()->all();;
  89. return $arr;
  90. }
  91. public function PidList($id = 0)
  92. {
  93. $query = self::find();
  94. $query->select(['name as title','id as value','pid','id']);
  95. $query->andWhere(['status'=>$this->setDel]);
  96. $query->andFilterWhere(['pid'=>$id]);
  97. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  98. }
  99. }