database icon indicating copy to clipboard operation
database copied to clipboard

Adding multiple data with a single query

Open zeydduran opened this issue 4 years ago • 3 comments

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');

zeydduran avatar Dec 22 '20 12:12 zeydduran

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?

msarca avatar Dec 22 '20 12:12 msarca

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');

dj12djdjs avatar Mar 31 '21 19:03 dj12djdjs

Any news about it? How can I use PDO::beginTransaction() and commit() to insert a multiple rows?

brunoinds avatar Apr 24 '21 18:04 brunoinds