Answer the question
In order to leave comments, you need to log in
How to save 100 articles and 200 related pictures in two requests in Yii2?
Good day to all. Whether prompt probably to save 300 records for two requests to basis.
For example, there are two tables: articles and pictures for them.
article
----------------------
id
title
description
article_image
---------------------- -
id
article_id
name
I receive an array of articles and links to pictures.
$articles = [
'article_1' => [
'title' => 'Title 1',
'description' => 'text 1',
'images' =>['name1.jpg','name2.jpg']
],
'article_2' => [
'title' => 'Title 2',
'description' => 'text 2',
'images' =>['name3.jpg','name4.jpg']
], // ...
];
\Yii::$app->db
->createCommand()
->batchInsert('article_image', ['article_id', 'name'], [
[1, 'name1.jpg'],
[2, 'name2.jpg'],
[3, 'name3.jpg'],
])
->execute();
Answer the question
In order to leave comments, you need to log in
the only option is to select max(id) from the article, lock the table, manually add ids starting from the maximum, insert, then unlock the table.
The solution is not very normal, because. "breaks" the normal operation of autoincrement, which is not good. But it will work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question