AdminConfig.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\models;
  10. use Yii;
  11. use fec\helpers\CDate;
  12. use yii\db\ActiveRecord;
  13. /**
  14. * User model
  15. *
  16. * @property integer $id
  17. * @property string $username
  18. * @property string $password_hash
  19. * @property string $password_reset_token
  20. * @property string $email
  21. * @property string $auth_key
  22. * @property integer $status
  23. * @property integer $created_at
  24. * @property integer $updated_at
  25. * @property string $password write-only password
  26. */
  27. /**
  28. * @author Terry Zhao <2358269014@qq.com>
  29. * @since 1.0
  30. */
  31. class AdminConfig extends ActiveRecord
  32. {
  33. /**
  34. * @inheritdoc
  35. */
  36. # 设置table
  37. public static function tableName()
  38. {
  39. return '{{%admin_config}}';
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. # 设置 status 默认 ,以及取值的区间
  45. public function rules()
  46. {
  47. return [
  48. ['label', 'filter', 'filter' => 'trim'],
  49. ['key', 'filter', 'filter' => 'trim'],
  50. ['value', 'filter', 'filter' => 'trim'],
  51. ['description', 'filter', 'filter' => 'trim'],
  52. ['key', 'valiadateKey'],
  53. ];
  54. }
  55. public function valiadateKey($attribute, $params){
  56. $key = $this->key;
  57. $id = $this->id;
  58. if($id){
  59. $one = AdminConfig::find()->where([
  60. "key" =>$key,
  61. ])
  62. ->andWhere(
  63. " id != :id ",[':id' => $id]
  64. )
  65. ->one()
  66. ;
  67. if($one['id']){
  68. $this->addError($attribute,"该条目已经存在[$key]");
  69. }
  70. }else{
  71. $one = AdminConfig::find()->where([
  72. "key" =>$key,
  73. ])
  74. ->one()
  75. ;
  76. if($one['id']){
  77. $this->addError($attribute,"该条目已经存在[$key]");
  78. }
  79. }
  80. }
  81. public function beforeSave($insert)
  82. {
  83. if (parent::beforeSave($insert)) {
  84. if($insert == self::EVENT_BEFORE_INSERT){
  85. $user = Yii::$app->user->identity;
  86. $account = $user['username'];
  87. $this->created_person = $account;
  88. $this->created_at = CDate::getCurrentDateTime();
  89. }else{
  90. }
  91. $this->updated_at = CDate::getCurrentDateTime();
  92. return true;
  93. } else {
  94. return false;
  95. }
  96. }
  97. }