UserRole.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/2/26/026
  6. * Time: 14:54
  7. */
  8. namespace common\models;
  9. //use yii\db\ActiveRecord;
  10. //use yii\behaviors\TimestampBehavior;
  11. //use Yii;
  12. class UserRole extends Common
  13. {
  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\UserRole','on'=>['add'],'message'=>'该部门名称已存在'],
  35. ['name','string','min'=>2,'max'=>20,'message'=>'{attribute}长度必须在{min}到{max}之间'],
  36. // ['describe','required','message'=>'部门描述不能为空'],
  37. ['describe','string','max'=>200,'message'=>'{attribute}长度必须在{min}到{max}之间'],
  38. ['state','in','range'=>[1,2]],
  39. ];
  40. }
  41. public function Check($input,$save = true)
  42. {
  43. $this->load($input,'');
  44. if(!$this->validate()) return $this->errors;
  45. return $this;
  46. }
  47. public function FindById($id)
  48. {
  49. return self::findOne($id);
  50. }
  51. public function getList($input)
  52. {
  53. $query = self::find();
  54. if(!empty($input['page']))
  55. {
  56. $query->offset = ($input['page'] -1) * $input['limit'];
  57. $query->limit = $input['limit'];
  58. }
  59. $row['count'] = $query->count();
  60. $row['data'] = $query->orderBy('create_at')->asArray()->all();
  61. return $row;
  62. }
  63. }