Answer the question
In order to leave comments, you need to log in
Yii2 can't catch up how to insert records when hasOne table relation?
The code is basically standard (controller method, post creation), except that 2 tables are used.
public function actionCreate()
{
$post = new Post();
$postInfo = new PostInfo();
//$post->link('postInfo', $postInfo);
if ($post->load(Yii::$app->request->post()) && $post->create()) {
return $this->redirect(['view', 'id' => $post->post_id]);
} else {
return $this->render('create', [
'model' => $post,
]);
}
}
Answer the question
In order to leave comments, you need to log in
> $post->link('postInfo', $postInfo);
You have to save first. Then the entities will have IDs and it will work.
> ... && $post->create()) {
->save()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question