Please test Nette Database 3.3 RC
- David Grudl
- founder | 8339
Hi, I've released nette/database 3.3.0-RC1 and I'd like to
ask you to test it. It brings some great new features, but what I care about
most is whether your existing code still works with it, especially inserting
rows, where the behavior of insert() has changed.
composer require nette/database:^3.3.0-RC1
It requires PHP 8.4 or newer and Tracy 2.12.
insert() and insertMany()
insert() has always returned the inserted row as an
ActiveRow. To do that, it sent a SELECT right after
the INSERT, even when you never used the result. And that's a very
common case: you insert a row and do nothing more with it, or read its ID
at most.
Now the row is loaded lazily. For a table with an integer primary key
(typically an auto-increment ID), the returned object holds only the ID and
sends the SELECT only when you touch one of the other columns. If
you don't read the row, no extra query is sent:
$row = $explorer->table('users')->insert([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
echo $row->id; // no extra query
echo $row->created_at; // only now is the row loaded from the database
So most inserts are now one query cheaper. But this touches a method that pretty much every application uses, which is why I'd love you to try it on your own code.
The values still come from the database, not from the array you passed in,
because the database may have changed them (column type, default value,
trigger). When the row can't be identified by its primary key (the table has
none, or part of a composite key is missing), insert() now
returns null.
To insert several rows in a single query, there's the new
insertMany(), which returns the number of inserted rows:
$count = $explorer->table('users')->insertMany([
['name' => 'John', 'year' => 1994],
['name' => 'Jack', 'year' => 1995],
]);
Passing several rows (or a Selection) directly to
insert() is deprecated. It still works exactly as
before, it just emits a deprecation notice, so you can switch to
insertMany() whenever it suits you.
Other behavior changes
Most applications won't notice them, but just in case:
ResultSet::fetch()consumes the row, so a followingforeachorfetchAll()starts with the next one. Previously it returned that row again.min(),max()andsum()over an empty group inGroupedSelectionreturnnullinstead of0, just like SQL and a plainSelectiondo.- A multi-insert via
?valueswarns when the rows don't have the same columns. Previously a missing value was silently filled with NULL and an extra column dropped. - A custom driver has to return the
scalekey fromgetColumns()as well.
The full list is in upgrading.md.
What's new
There's quite a lot of new stuff this time and I think you're going to love it. I'll introduce it properly in a blog post, so here's just a taste:
- entity mapping: Explorer can return rows as your own
classes (
ProductRowextendingActiveRow) and optionally translate acreated_atcolumn to a$row->createdAtproperty transaction()can automatically retry the whole transaction after a deadlock or a lock timeout- AI agents can now read the Tracy panel too, plus
isInTransaction()and a few smaller things
You can read about all of it in the documentation today, for example entity mapping or retrying transactions.
Just install the RC, clear the cache and click through your application, or run your tests. If something stops working or you run into unexpected deprecation notices or warnings, please let me know. Thanks!