Countries.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%COUNTRIES}}".
  7. *
  8. * @property string ID
  9. * @property string NAME 名字
  10. * @property string CODE 简码
  11. * @property string LOCAL_CURRENCY_ID 货币ID
  12. * @property string DEFAULT_LANGUAGE_ID 语言ID
  13. * @property string LANGUAGE_KEY 语言KEY
  14. * @property int ACTIVE 状态
  15. * @property int CREATED_AT 创建时间
  16. * @property int UPDATED_AT 更新时间
  17. */
  18. class Countries extends \common\components\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%COUNTRIES}}';
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['ID'], 'unique'],
  34. ];
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'ID' => 'ID',
  43. 'NAME' => '名字',
  44. 'CODE' => '简码',
  45. 'LOCAL_CURRENCY_ID' => '货币ID',
  46. 'DEFAULT_LANGUAGE_ID' => '语言ID',
  47. 'ACTIVE' => '状态:1正常 0异常',
  48. 'LANGUAGE_KEY' => '语言KEY',
  49. 'CREATED_AT' => '创建时间',
  50. 'UPDATED_AT' => '更新时间',
  51. ];
  52. }
  53. /**
  54. * 从缓存获取信息
  55. * @return array|mixed|\yii\db\ActiveRecord[]
  56. */
  57. public static function getFromCache()
  58. {
  59. $data = Yii::$app->cache->get(Cache::COUNTRIES_KEY);
  60. if (!$data) {
  61. // 获取信息
  62. $data = static::find()->orderBy('ACTIVE DESC, ID ASC')->indexBy('ID')->asArray()->all();
  63. Yii::$app->cache->set(Cache::COUNTRIES_KEY, $data);
  64. }
  65. // i18n转换
  66. foreach ($data as &$item) {
  67. $item['COUNTRY_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
  68. }
  69. return $data;
  70. }
  71. /**
  72. * @return array|\yii\db\ActiveRecord[]
  73. */
  74. public static function getAllData()
  75. {
  76. return static::find()->where('1=1')->indexBy('ID')->orderBy('ACTIVE DESC, ID ASC')->asArray()->all();
  77. }
  78. /**
  79. * 更新缓存
  80. * @return array|\yii\db\ActiveRecord[]
  81. */
  82. public static function updateToCache()
  83. {
  84. // 获取配置
  85. $data = static::find()->where('1=1')->orderBy('ACTIVE DESC, ID ASC')->asArray()->indexBy('ID')->all();
  86. Yii::$app->cache->set(Cache::COUNTRIES_KEY, $data);
  87. return $data;
  88. }
  89. /**
  90. * 通过ID获取
  91. * @param string $id
  92. * @return array|null
  93. */
  94. public static function getById(string $id)
  95. {
  96. return self::findOneAsArray('ID=:ID', [':ID' => $id]);
  97. }
  98. }