MenuCate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace common\models\backend;
  3. use Yii;
  4. use common\enums\WhetherEnum;
  5. use common\helpers\TreeHelper;
  6. use common\enums\StatusEnum;
  7. /**
  8. * This is the model class for table "rf_common_menu_cate".
  9. *
  10. * @property int $id 主键
  11. * @property string $title 标题
  12. * @property string $app_id 应用
  13. * @property string $addons_name 插件名称
  14. * @property string $icon icon
  15. * @property int $is_default_show 默认显示
  16. * @property int $is_addon 默认非插件顶级分类
  17. * @property int $sort 排序
  18. * @property int $level 级别
  19. * @property int $addon_centre
  20. * @property string $tree 树
  21. * @property string $pid 上级id
  22. * @property int $status 状态[-1:删除;0:禁用;1启用]
  23. * @property string $created_at 添加时间
  24. * @property string $updated_at 修改时间
  25. */
  26. class MenuCate extends \common\models\base\BaseModel
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%menu_cate}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['title'], 'required'],
  42. [['level', 'pid', 'addon_centre', 'is_default_show', 'is_addon', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['title'], 'string', 'max' => 50],
  44. [['app_id', 'icon'], 'string', 'max' => 20],
  45. [['addons_name'], 'string', 'max' => 100],
  46. [['tree'], 'string', 'max' => 300],
  47. [['level'], 'default', 'value' => 1],
  48. [['title', 'icon'], 'trim'],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'title' => '标题',
  59. 'app_id' => '应用',
  60. 'addons_name' => '插件名称',
  61. 'icon' => '图标',
  62. 'is_default_show' => '是否默认显示',
  63. 'is_addon' => '应用中心',
  64. 'sort' => '排序',
  65. 'level' => '级别',
  66. 'pid' => '上级id',
  67. 'tree' => '树',
  68. 'addon_centre' => '应用中心',
  69. 'status' => '状态',
  70. 'created_at' => '创建时间',
  71. 'updated_at' => '修改时间',
  72. ];
  73. }
  74. /**
  75. * @param bool $insert
  76. * @return bool
  77. */
  78. public function beforeSave($insert)
  79. {
  80. if ($this->is_default_show == StatusEnum::ENABLED) {
  81. self::updateAll(['is_default_show' => StatusEnum::DISABLED], ['is_default_show' => StatusEnum::ENABLED, 'app_id' => $this->app_id]);
  82. }
  83. if ($this->isNewRecord) {
  84. !$this->app_id && $this->app_id = Yii::$app->id;
  85. !$this->is_addon && $this->is_addon = WhetherEnum::DISABLED;
  86. $this->pid == 0 && $this->tree = TreeHelper::defaultTreeKey();
  87. }
  88. return parent::beforeSave($insert);
  89. }
  90. }