Config.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 fecshop\models\mysqldb\admin;
  10. use Yii;
  11. use fec\helpers\CDate;
  12. use yii\db\ActiveRecord;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Config extends ActiveRecord
  18. {
  19. /**
  20. * @inheritdoc
  21. */
  22. # 设置table
  23. public static function tableName()
  24. {
  25. return '{{%admin_config}}';
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. # 设置 status 默认 ,以及取值的区间
  31. public function rules()
  32. {
  33. return [
  34. ['label', 'filter', 'filter' => 'trim'],
  35. ['key', 'filter', 'filter' => 'trim'],
  36. ['value', 'filter', 'filter' => 'trim'],
  37. ['description', 'filter', 'filter' => 'trim'],
  38. ['key', 'valiadateKey'],
  39. ];
  40. }
  41. public function valiadateKey($attribute, $params){
  42. $key = $this->key;
  43. $id = $this->id;
  44. if($id){
  45. $one = self::find()->where([
  46. "key" =>$key,
  47. ])
  48. ->andWhere(
  49. " id != :id ",[':id' => $id]
  50. )
  51. ->one()
  52. ;
  53. if($one['id']){
  54. $this->addError($attribute,"该条目已经存在[$key]");
  55. }
  56. }else{
  57. $one = self::find()->where([
  58. "key" =>$key,
  59. ])
  60. ->one()
  61. ;
  62. if($one['id']){
  63. $this->addError($attribute,"该条目已经存在[$key]");
  64. }
  65. }
  66. }
  67. public function beforeSave($insert)
  68. {
  69. if (parent::beforeSave($insert)) {
  70. if ($insert == self::EVENT_BEFORE_INSERT) {
  71. $user = Yii::$app->user->identity;
  72. $account = $user['username'];
  73. $this->created_person = $account;
  74. $this->created_at = CDate::getCurrentDateTime();
  75. }
  76. $this->updated_at = CDate::getCurrentDateTime();
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. }