feat: add Query Builder insertGetID()#10260
Conversation
- add insertGetID() to Query Builder - return false when no insert result or no row is inserted - prevent Model from proxying the builder method - document the new API and cover core behavior with tests Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
michalsn
left a comment
There was a problem hiding this comment.
We should document that this new method cannot be used with prepared statements.
I also find insertGetID() slightly awkward, but I cannot think of a better alternative.
Also, I am not entirely convinced that we need this in Query Builder, but I would like to hear what others think.
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
I updated the docs to mention the prepared-query limitation. About the API itself, I agree this is different from things like I’m happy to hear what others think and close this if undesired. |
|
I took think that |
Description
This PR proposes adding
insertGetID()to Query Builder. This is a small API, but it removes a very common bit of friction: inserting a row and then immediately asking the connection for the generated ID.Instead of writing:
users can write:
That keeps the intent in one place: "insert this row and give me its ID". It is easier to read, harder to misuse, and avoids making users jump from the builder back to the connection for the second half of the same operation.
The method stays deliberately thin. It uses the existing
insert()flow, then returns the database driver's existinginsertID()value. It returnsfalsewhen the insert fails, when no row is inserted, or when the builder is running in test/pretend mode.I also blocked this from being proxied through
Model, since Model already has its own insert flow with validation, events, timestamps, and return-ID behavior. This keeps the new method scoped to Query Builder, where it belongs.Tests cover successful inserts, insert failures, no affected rows, test/pretend mode, and the Model guard.
Checklist: