A
A
AlikDex2016-01-22 11:17:22
Yii
AlikDex, 2016-01-22 11:17:22

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,
    ]);
  }
}

Here's how to link them when creating? I thought the link would help, but no, it asks the primary key to indicate (i.e. the id must be defined).
Or still send 2 models to the form?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Khomenko, 2016-01-22
@AlikDex

> $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 question

Ask a Question

731 491 924 answers to any question