Skip to content

Fix multi-line insert return type - #330

Merged
dg merged 3 commits into
nette:masterfrom
KminekMatej:fix/multi-insert-returns-int
Jun 6, 2026
Merged

dg merged 3 commits into
nette:masterfrom
KminekMatej:fix/multi-insert-returns-int

Conversation

@KminekMatej

Copy link
Copy Markdown
Contributor

Multi-line inserting wasnt returning numeric number of affected rows but ActiveRow instead (with first inserted record).

This fix checks if the $data inserted is actually a two-dimensional array and if so, returns number of affected rows instead of the ActiveRow.

Tests were added as well since this path was not guarded

@dg

dg commented Jun 6, 2026

Copy link
Copy Markdown
Member

Thanks

@dg
dg merged commit 986e24a into nette:master Jun 6, 2026
dg pushed a commit that referenced this pull request Jun 7, 2026
dg pushed a commit that referenced this pull request Jun 7, 2026
dg pushed a commit that referenced this pull request Jun 8, 2026
dg pushed a commit that referenced this pull request Jul 16, 2026
dg pushed a commit that referenced this pull request Jul 16, 2026
dg pushed a commit that referenced this pull request Jul 16, 2026
dg pushed a commit that referenced this pull request Jul 16, 2026
dg added a commit that referenced this pull request Jul 16, 2026
… multi-insert (#330)

Multi-line inserting wasn't returning the number of affected rows but an
ActiveRow instead (with the first inserted record). The documented behaviour
is: a single associative array inserts one row and returns the ActiveRow,
a list of rows or a Selection performs a bulk insert and returns the count.

A list is recognized by its keys being integers - they need not be sequential,
since a filtered array leaves gaps in them, and an integer is never a column
name. Such rows are reindexed before the query. Rows coming from a Traversable
are drained by position, so none is lost when a generator yields them under
colliding keys (`yield from`). An empty array stays a single insert of database
defaults, so an unfilled form ($form->getValues()) still inserts a row.

Both checks live in Helpers::materializeRows() and Helpers::isRowList(), so
that insert() and GroupedSelection share one notion of what a list of rows is.

forum thread: https://forum.nette.org/cs/36954-nette-database-v3-2-9-phpdoc-selection-insert-nepokryva-dokumentovany-bulk-insert

Co-authored-by: Matěj Kmínek <m@kminet.eu>
dg added a commit that referenced this pull request Aug 27, 2026
… multi-insert (#330)

Multi-line inserting wasn't returning the number of affected rows but an
ActiveRow instead (with the first inserted record). The documented behaviour
is: a single associative array inserts one row and returns the ActiveRow,
a list of rows or a Selection performs a bulk insert and returns the count.

A list is recognized by its keys being integers - they need not be sequential,
since a filtered array leaves gaps in them, and an integer is never a column
name. Such rows are reindexed before the query. Rows coming from a Traversable
are drained by position, so none is lost when a generator yields them under
colliding keys (`yield from`). An empty array stays a single insert of database
defaults, so an unfilled form ($form->getValues()) still inserts a row.

Both checks live in Helpers::materializeRows() and Helpers::isRowList(), so
that insert() and GroupedSelection share one notion of what a list of rows is.

forum thread: https://forum.nette.org/cs/36954-nette-database-v3-2-9-phpdoc-selection-insert-nepokryva-dokumentovany-bulk-insert

Co-authored-by: Matěj Kmínek <m@kminet.eu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants