S
S
Sergey Beloventsev2016-05-03 20:10:00
Yii
Sergey Beloventsev, 2016-05-03 20:10:00

How to find category id?

I want to write a parser that receives information from the site, it turns out such an array

$array(
    [category]=>'Loren ipsum'
    [description]=>'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'
    [name]=>'За словесными горами'
    [desc]=>'Далеко-далеко за словесными горами в стране гласных и согласных живут рыбные тексты. Вдали от всех живут они в буквенных домах на берегу Семантика большого языкового океана.'
    [cat]=>'Lorem ipsum');

there are category and post models with the following content:
category ->id, name, description;
post ->id,name,description,id_category.
Accordingly, in the controller, I will write to the database like this
$category= new Category();
    $post =new Post();
     if ($model->load(Yii::$app->request->post()) ) {
      $category->name = $array['category'];
      $category->description=$array['description'];
      $category->save();
      $post->name=$array['name'];
      $post->description=$array['desc'];
    }

the question is how do I find out the id of this $category just created here in order to write it to the id_category of the post?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rustamka Vorontsov, 2016-05-03
@Sergalas

I think you can use the relay www.yiiframework.com/doc-2.0/guide-db-active-recor...
1. record id is obtained after ->save(true)
2. if all else fails, then read about before after save() method

R
Roman, 2016-05-03
@r_zaycev

So get:
Either
Doki

M
Maxim Timofeev, 2016-05-04
@webinar

$category= new Category();
$post =new Post();
$category->save();
$post->category_id = $category->id;
$post->save();

Is the idea clear?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question