Config.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace common\modules\config\models;
  3. use common\behaviors\CacheInvalidateBehavior;
  4. use common\modules\attachment\models\Attachment;
  5. use yii\behaviors\TimestampBehavior;
  6. /**
  7. * This is the model class for table "{{%config}}".
  8. *
  9. * @property int $id
  10. * @property string $name
  11. * @property string $value
  12. * @property string $type
  13. * @property string $desc
  14. */
  15. class Config extends \yii\db\ActiveRecord
  16. {
  17. const TYPE_ARRAY = 'array';
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%config}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['name', 'description', 'type'], 'required'],
  32. [['name', 'group'], 'string', 'max' => 50],
  33. ['type', 'in', 'range' => array_keys(self::getTypeList())],
  34. ['value', 'filter', 'filter' => function ($val) {
  35. if ($this->type == 'checkbox') {
  36. return serialize($val);
  37. }
  38. return $val;
  39. }, 'skipOnEmpty' => true],
  40. [['value', 'description', 'extra'], 'string'],
  41. ];
  42. }
  43. public function afterFind()
  44. {
  45. parent::afterFind();
  46. if ($this->type == 'checkbox' && !empty($this->value)) {
  47. $this->value = unserialize($this->value);
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'name' => '配置名',
  58. 'value' => '配置值',
  59. 'description' => '配置描述',
  60. 'type' => '配置类型',
  61. 'extra' => '配置项',
  62. 'group' => '分组',
  63. ];
  64. }
  65. public function behaviors()
  66. {
  67. return [
  68. TimestampBehavior::className(),
  69. [
  70. 'class' => CacheInvalidateBehavior::className(),
  71. 'tags' => [
  72. \Yii::$app->config->cacheTag
  73. ]
  74. ]
  75. ];
  76. }
  77. public static function getTypeList()
  78. {
  79. return \Yii::$app->config->get('config_type_list');
  80. }
  81. }