/** * @return string|Response * @throws DbException */ public function actionAsk() { $model = new Question(); $userModel = new QaUser(); if ($model->load($_POST) && $userModel->load($_POST)) { $valid = $model->validate(); $valid = $userModel->validate() && $valid; if (!$valid) { return $this->render('ask', compact(['model', 'userModel'])); } if (!$model->save()) { throw new DbException(Module::t('main', 'Error create question')); } if (!$userModel->save()) { throw new DbException(Module::t('main', 'Error save username')); } $model->user_id = $userModel->id; if (!$model->save()) { throw new DbException(Module::t('main', 'Error saving user id')); } Yii::$app->session->setFlash('questionFormSubmitted'); return $this->redirect(Url::to(['view', 'id' => $model->id, 'alias' => $model->alias])); } else { return $this->render('ask', compact(['model', 'userModel'])); } }
/** * @param $name * @return QaUser|bool */ public function createUser($name) { $newUser = new QaUser(); $newUser->username = $name; if ($newUser->save()) { return $newUser; } return false; }