/**
  * Evaluates the value of the user.
  * The return result of this method will be assigned to the current attribute(s).
  * @param Event $event
  * @return mixed the value of the user.
  */
 protected function getValue($event)
 {
     $attribute = $this->subdistrictAttribute;
     $value = $this->owner->{$attribute};
     $parentAttribute = $this->districtAttribute;
     $parent_id = $this->owner->{$parentAttribute};
     $parent_valid = $parent_id > 0;
     if (is_numeric($value)) {
         return $value;
     } else {
         if (empty($value) or $parent_valid == FALSE) {
             return NULL;
         } else {
             $model = new RgnSubdistrict(['name' => $value, 'district_id' => $parent_id, 'recordStatus' => RgnSubdistrict::RECORDSTATUS_USED]);
             return $model->save(FALSE) ? $model->id : 0;
         }
     }
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [['recordStatus', 'default', 'value' => static::RECORDSTATUS_USED], [['postcode', 'country_id'], 'required'], [['subdistrict_id', 'district_id', 'city_id', 'province_id'], 'safe'], [['recordStatus'], 'string'], [['postcode'], 'integer'], ['recordStatus', 'in', 'range' => [self::RECORDSTATUS_USED, self::RECORDSTATUS_DELETED]], ['country_id', 'exist', 'targetClass' => RgnCountry::className(), 'targetAttribute' => 'id', 'when' => function (RgnPostcode $model, $attribute) {
         $num = is_numeric($model->{$attribute});
         //$model->addError($attribute, "num: [{$num}]; val: [{$model->$attribute}];");
         return $num;
     }, 'message' => "Country doesn't exist."], ['province_id', 'exist', 'targetClass' => RgnProvince::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) {
         return is_numeric($model->{$attribute});
     }, 'message' => "Province doesn't exist."], ['city_id', 'exist', 'targetClass' => RgnCity::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) {
         return is_numeric($model->{$attribute});
     }, 'message' => "City doesn't exist."], ['district_id', 'exist', 'targetClass' => RgnDistrict::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) {
         return is_numeric($model->{$attribute});
     }, 'message' => "District doesn't exist."], ['subdistrict_id', 'exist', 'targetClass' => RgnSubdistrict::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) {
         return is_numeric($model->{$attribute});
     }, 'message' => "Subdistrict doesn't exist."]];
 }
Example #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSubdistrict()
 {
     return $this->hasOne(\common\models\RgnSubdistrict::className(), ['id' => 'subdistrict_id']);
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->operation = new RgnSubdistrictOperation(['model' => $this]);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getRgnSubdistricts()
 {
     return $this->hasMany(\common\models\RgnSubdistrict::className(), ['district_id' => 'id']);
 }