* @copyright (c), Thread */ class City extends ActiveRecord { /** * @inheritdoc */ public static function getDb() { return LocationModule::getDb(); } /** * @return string */ public static function tableName() { return '{{%location_city}}'; } /** * @inheritdoc * @return array */ public function behaviors() { return ArrayHelper::merge( parent::behaviors(), [ [ 'class' => AttributeBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'alias', ActiveRecord::EVENT_BEFORE_UPDATE => 'alias', ], 'value' => function ($event) { return Inflector::slug($this->alias); }, ], ] ); } /** * @return array */ public function rules() { return [ [['alias'], 'required'], [['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())], [['country_id', 'created_at', 'updated_at'], 'integer'], [['alias', 'default_title'], 'string', 'max' => 255], [['alias'], 'unique'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => Yii::t('app', 'ID'), 'alias' => Yii::t('app', 'Alias'), 'country_id' => Yii::t('location', 'Country'), 'created_at' => Yii::t('app', 'Create time'), 'updated_at' => Yii::t('app', 'Update time'), 'published' => Yii::t('app', 'Published'), 'deleted' => Yii::t('app', 'Deleted'), 'title' => Yii::t('app', 'Title'), 'default_title' => Yii::t('app', 'Default Title'), ]; } /** * * @return array */ public function scenarios() { return [ 'published' => ['published'], 'deleted' => ['deleted'], 'backend' => ['alias', 'country_id', 'created_at', 'updated_at', 'published', 'deleted', 'search_title', 'default_title'], ]; } /** * @return \yii\db\ActiveQuery */ public function getLang() { return $this->hasOne(CityLang::class, ['rid' => 'id']); } /** * @return string */ public function getTitle() { return (isset($this->lang->title)) ? $this->lang->title : "{{$this->default_title}}"; } /** * @return \yii\db\ActiveQuery */ public function getCountry() { return $this->hasOne(Country::class, ['id' => 'country_id']); } }