/** * 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->districtAttribute; $value = $this->owner->{$attribute}; $parentAttribute = $this->cityAttribute; $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 District(['name' => $value, 'city_id' => $parent_id, 'status' => District::STATUS_ACTIVE]); return $model->save(FALSE) ? $model->id : 0; } } }
/** * Creates a new District model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (DistrictAccess::allowCreate() == FALSE) { throw DistrictAccess::exception('create'); } $model = new District(); try { if ($model->load($_POST) && $model->save()) { return $this->redirect(Url::previous()); } elseif (!\Yii::$app->request->isPost) { $model->load($_GET); } } catch (\Exception $e) { $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage(); $model->addError('_exception', $msg); } return $this->render('create', ['model' => $model]); }