Language.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if (!$data) {
  61. // 获取信息
  62. $data = static::find()->orderBy('ORDER DESC, ACTIVE ASC')->indexBy('ID')->asArray()->all();
  63. Yii::$app->cache->set(Cache::LANGUAGE_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('ORDER DESC, ACTIVE 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('ORDER DESC, ACTIVE ASC')->asArray()->indexBy('ID')->all();
  86. Yii::$app->cache->set(Cache::LANGUAGE_KEY, $data);
  87. return $data;
  88. }
  89. /**
  90. * 通过ID获取
  91. * @param $id
  92. * @return array|null
  93. */
  94. public static function getById($id)
  95. {
  96. return self::findOneAsArray('ID=:ID', [':ID' => $id]);
  97. }
  98. public static function getEn()
  99. {
  100. $data = self::findOneAsArray('ID=:ID', [':ID' => '620713695651307520']);
  101. return $data['ID'];
  102. }
  103. }