Skip to content

Commit 2d369bc

Browse files
committed
Selection::insert(): multi insert returns number of affected rows instead of data (BC break)
1 parent 3357b99 commit 2d369bc

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Database/Table/Selection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public function getDataRefreshed()
744744
/**
745745
* Inserts row in a table.
746746
* @param array|\Traversable|Selection array($column => $value)|\Traversable|Selection for INSERT ... SELECT
747-
* @return IRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key
747+
* @return IRow|int|bool Returns IRow or number of affected rows for Selection, multi insert or table without primary key
748748
*/
749749
public function insert($data)
750750
{
@@ -766,7 +766,7 @@ public function insert($data)
766766
}
767767

768768
if (Nette\Utils\Arrays::isList($data)) {
769-
return $data;
769+
return $return->getRowCount();
770770
}
771771

772772
$primaryKey = [];

tests/Database/Table/Selection.insert().multi.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverN
1414

1515
test(function () use ($context) {
1616
Assert::same(3, $context->table('author')->count());
17-
$context->table('author')->insert([
17+
$insert = $context->table('author')->insert([
1818
[
1919
'name' => 'Catelyn Stark',
2020
'web' => 'http://example.com',
@@ -26,15 +26,17 @@ test(function () use ($context) {
2626
'born' => new DateTime('2021-11-11'),
2727
],
2828
]); // INSERT INTO `author` (`name`, `web`, `born`) VALUES ('Catelyn Stark', 'http://example.com', '2011-11-11 00:00:00'), ('Sansa Stark', 'http://example.com', '2021-11-11 00:00:00')
29+
Assert::same(2, $insert);
2930
Assert::same(5, $context->table('author')->count());
3031

3132
$context->table('book_tag')->where('book_id', 1)->delete(); // DELETE FROM `book_tag` WHERE (`book_id` = ?)
3233

3334
Assert::same(4, $context->table('book_tag')->count());
34-
$context->table('book')->get(1)->related('book_tag')->insert([ // SELECT * FROM `book` WHERE (`id` = ?)
35+
$insert = $context->table('book')->get(1)->related('book_tag')->insert([ // SELECT * FROM `book` WHERE (`id` = ?)
3536
['tag_id' => 21],
3637
['tag_id' => 22],
3738
['tag_id' => 23],
3839
]); // INSERT INTO `book_tag` (`tag_id`, `book_id`) VALUES (21, 1), (22, 1), (23, 1)
40+
Assert::same(3, $insert);
3941
Assert::same(7, $context->table('book_tag')->count());
4042
});

0 commit comments

Comments
 (0)