Language.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%LANGUAGES}}".
  7. *
  8. * @property string $ID
  9. * @property string $NAME 名字
  10. * @property string $CODE 简码
  11. * @property string KEY KEY
  12. * @property string LOCAL_CODE 简码
  13. * @property int $ORDER 排序
  14. * @property int $ACTIVE 状态
  15. * @property int $CREATED_AT 创建时间
  16. * @property int $UPDATED_AT 更新时间
  17. */
  18. class Language extends \common\components\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%LANGUAGES}}';
  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. 'KEY' => 'KEY',
  45. 'LOCAL_CODE' => '简码',
  46. 'ORDER' => '排序值',
  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::LANGUAGE_KEY);
  60. Yii::$app->cache->delete(Cache::LANGUAGE_KEY);
  61. if (!$data) {
  62. // 获取信息
  63. $data = self::getAllData();
  64. Yii::$app->cache->set(Cache::LANGUAGE_KEY, $data);
  65. }
  66. // i18n转换
  67. // foreach ($data as &$item) {
  68. // $item['COUNTRY_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
  69. // }
  70. return $data;
  71. }
  72. /**
  73. * @return array|\yii\db\ActiveRecord[]
  74. */
  75. public static function getAllData()
  76. {
  77. return static::find()->where('ACTIVE=1')->indexBy('ID')->orderBy('ORDER DESC, ACTIVE ASC')->asArray()->all();
  78. }
  79. /**
  80. * 更新缓存
  81. * @return array|\yii\db\ActiveRecord[]
  82. */
  83. public static function updateToCache()
  84. {
  85. // 获取配置
  86. $data = self::getAllData();
  87. Yii::$app->cache->set(Cache::LANGUAGE_KEY, $data);
  88. return $data;
  89. }
  90. /**
  91. * 通过ID获取
  92. * @param $id
  93. * @return array|null
  94. */
  95. public static function getById($id)
  96. {
  97. return self::findOneAsArray('ID=:ID', [':ID' => $id]);
  98. }
  99. public static function getEn()
  100. {
  101. $data = self::findOneAsArray('ID=:ID', [':ID' => '620713695651307520']);
  102. return $data['ID'];
  103. }
  104. }