GlobalSetting.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace MGModule\DNSManager2\models\custom\globalsetting;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Log class
  6. *
  7. * @Table(name=globalsetting)
  8. */
  9. class GlobalSetting extends main\mgLibs\models\orm{
  10. /**
  11. * ID field
  12. *
  13. * @Column(id)
  14. * @var int
  15. */
  16. public $id;
  17. /**
  18. *
  19. * @Column(varchar,uniqueKey=1,isKey=1)
  20. * @var int
  21. */
  22. public $key;
  23. /**
  24. *
  25. * @Column(text)
  26. * @var string
  27. */
  28. public $value;
  29. public function __toString() {
  30. return $this->value;
  31. }
  32. public function save($data = array()) {
  33. if(!GlobalSettingEnum::isValidValue($this->key)) {
  34. throw new \Exception('Invalid Global Setting Key (' . $this->key . ')');
  35. }
  36. parent::save($data);
  37. }
  38. public static function byKey($key) {
  39. return Repository::factory()->byKey($key)->one();
  40. }
  41. public static function set($key, $value) {
  42. if(!$setting = self::byKey($key)) {
  43. $setting = new GlobalSetting();
  44. $setting->key = $key;
  45. }
  46. $setting->value = is_null($value) || $value === false?'':$value;
  47. $setting->save();
  48. return $setting;
  49. }
  50. }