Код: Выделить всё
public function actionSignup()
{
$user = new User(['scenario' => 'signup']);
$profile = new Profile();
if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) {
if ($user->validate() && $profile->validate()) {
$user->populateRelation('profile', $profile);
if ($user->save(false)) {
if ($this->module->requireEmailConfirmation === true) {
Yii::$app->session->setFlash('success', Yii::t('users.flash', 'SUCCESS_SIGNUP_WITHOUT_LOGIN', [
'url' => Url::toRoute('resend')
]));
Yii::$app->consoleRunner->run('users/send-mail ' . $user->id . ' signup');
} else {
Yii::$app->user->login($user);
Yii::$app->session->setFlash('success', Yii::t('users.flash', 'SUCCESS_SIGNUP_WITH_LOGIN'));
}
return $this->redirect('login');
} else {
Yii::$app->session->setFlash('danger', Yii::t('users.flash', 'FAIL_SIGNUP'));
return $this->refresh();
}
} else if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($user);
}
}
return $this->render('signup.twig', [
'user' => $user,
'profile' => $profile
]);
}
Форма регистрации использует как поля модули User так и поля модели Profile.
Валидация проходит все ок. Когда доходит дело до сохранения, я пытаюсь добавить профиль вот таким образом:
Код: Выделить всё
$user->populateRelation('profile', $profile);
Код: Выделить всё
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`test`.`profiles`, CONSTRAINT `FK_profile_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
The SQL being executed was: INSERT INTO `profiles` (`name`, `surname`, `whau`) VALUES ('Иван', 'Иванов', 'Соц. Сети')
Собственно реляция в модели User и Profile есть.
Подкажите пожалуйста как решить проблему ?