Region.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\ArrayHelper;
  5. /**
  6. * This is the model class for table "{{%region}}".
  7. *
  8. * @property string $id
  9. * @property string $name
  10. * @property integer $parent_id
  11. * @property integer $level
  12. */
  13. class Region extends \yii\db\ActiveRecord
  14. {
  15. /**
  16. * @inheritdoc
  17. */
  18. public static function tableName()
  19. {
  20. return '{{%region}}';
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['id', 'name', 'parent_id', 'level'], 'required'],
  29. [['id', 'parent_id', 'level'], 'integer'],
  30. [['name'], 'string', 'max' => 100],
  31. ];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function attributeLabels()
  37. {
  38. return [
  39. 'id' => Yii::t('common', 'ID'),
  40. 'name' => Yii::t('common', 'Name'),
  41. 'parent_id' => Yii::t('common', 'Parent ID'),
  42. 'level' => Yii::t('common', 'Level'),
  43. ];
  44. }
  45. /**
  46. * @inheritdoc
  47. * @return \common\models\query\RegionQuery the active query used by this AR class.
  48. */
  49. public static function find()
  50. {
  51. return new \common\models\query\RegionQuery(get_called_class());
  52. }
  53. public static function getRegion($parentId = 0)
  54. {
  55. $result = static::find()->where(['parent_id' => $parentId])->asArray()->all();
  56. return ArrayHelper::map($result, 'id', 'name');
  57. }
  58. public static function getNameById($id = 0)
  59. {
  60. $name = '';
  61. $result = static::findOne(['id'=>$id]);
  62. if($result){
  63. $name = $result->name;
  64. }
  65. return $name;
  66. }
  67. }