AuthItem.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace common\models\rbac;
  3. use common\traits\Tree;
  4. /**
  5. * This is the model class for table "{{%rbac_auth_item}}".
  6. *
  7. * @property int $id
  8. * @property string $name 别名
  9. * @property string|null $title 标题
  10. * @property string $app_id 应用
  11. * @property int|null $pid 父级id
  12. * @property int|null $level 级别
  13. * @property int|null $is_addon 是否插件
  14. * @property string|null $addon_name 插件名称
  15. * @property int|null $sort 排序
  16. * @property string|null $tree 树
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int|null $created_at 创建时间
  19. * @property int|null $updated_at 修改时间
  20. */
  21. class AuthItem extends \common\models\base\BaseModel
  22. {
  23. use Tree;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%rbac_auth_item}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['title', 'name'], 'required'],
  38. [['name'], 'uniqueName'],
  39. [['pid', 'level', 'is_addon', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['name'], 'string', 'max' => 64],
  41. [['title', 'addon_name'], 'string', 'max' => 200],
  42. [['app_id'], 'string', 'max' => 20],
  43. [['tree'], 'string', 'max' => 500],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'name' => '别名',
  54. 'title' => '标题',
  55. 'app_id' => '应用',
  56. 'pid' => '父级',
  57. 'level' => '级别',
  58. 'is_addon' => '是否插件',
  59. 'addon_name' => '插件名称',
  60. 'sort' => '排序',
  61. 'tree' => '树',
  62. 'status' => '状态[-1:删除;0:禁用;1启用]',
  63. 'created_at' => '创建时间',
  64. 'updated_at' => '修改时间',
  65. ];
  66. }
  67. /**
  68. * @param AuthItem $parent
  69. */
  70. public function setParent(AuthItem $parent)
  71. {
  72. $this->parent = $parent;
  73. }
  74. /**
  75. * @param $attribute
  76. */
  77. public function uniqueName($attribute)
  78. {
  79. $model = self::find()
  80. ->where(['name' => $this->name, 'app_id' => $this->app_id])
  81. ->andFilterWhere(['addon_name' => $this->addon_name])
  82. ->one();
  83. if ($model && $model->id != $this->id) {
  84. $this->addError($attribute, '别名已存在');
  85. }
  86. }
  87. /**
  88. * @param bool $insert
  89. * @param array $changedAttributes
  90. */
  91. public function afterSave($insert, $changedAttributes)
  92. {
  93. if (!$this->isNewRecord) {
  94. AuthItemChild::updateAll(['name' => $this->name], ['item_id' => $this->id]);
  95. }
  96. parent::afterSave($insert, $changedAttributes);
  97. }
  98. }
粤ICP备19079148号