H
H
happ2016-08-06 20:04:19
Yii
happ, 2016-08-06 20:04:19

How to properly create a prepared query to add multiple values ​​in yii2?

Such a request works correctly, I add the data in a cycle, but this is not the best option, because every request is parsed. How to insert an array of values. I use other methods - I get errors.
Yii::$app->db->createCommand()->insert('post_tag', [
'post_id' => $postId,
'tag_id' => $tag,
])->execute();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-08-06
@happ

Yii::$app->db->createCommand()->batchInsert('user', ['name', 'age'], [
    ['Tom', 30],
    ['Jane', 20],
    ['Linda', 25],
])->execute();

example from documentation www.yiiframework.com/doc-2.0/guide-db-dao.html
Here is another more detailed version stackoverflow.com/questions/27355262/activerecord-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question