database
database copied to clipboard
Adding multiple data with a single query
example :
$db->insert( [ 'tag' => 'asd', 'score' => rand() ], [ 'tag' => 'asd2', 'score' => rand() ], [ 'tag' => 'asd3', 'score' => rand() ], )->into('tags');
Or $db->insert([ [ 'tag' => 'asd', 'score' => rand() ], [ 'tag' => 'asd2', 'score' => rand() ], [ 'tag' => 'asd3', 'score' => rand() ], ])->into('tags');
We had something like this in 2013, but we removed it. I can’t remember why. Probably it was some sort of compatibility issue between different database systems. I believe that now it is safe to have something like this in the next release targeting PHP 8. @sorinsarca any thoughts on this?
I've seen an implementation similar to seen below in other libraries I've worked with.
/** @var \Opis\Database\SQL\InsertStatement $insert **/
$insert = $db->insert(columns: ['col1', 'col2', 'col3']);
foreach ($list as $record) {
$insert->values([
'col1' => $record['col1'],
'col2' => $record['col2'],
'col3' => $record['col3']
]);
}
$insert->into('table_name');
Any news about it? How can I use PDO::beginTransaction() and commit() to insert a multiple rows?