Skip to content

feat: add Query Builder insertGetID()#10260

Open
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-insert-get-id
Open

feat: add Query Builder insertGetID()#10260
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-insert-get-id

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

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:

$builder->insert($data);
$insertID = $db->insertID();

users can write:

$insertID = $builder->insertGetID($data);

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 existing insertID() value. It returns false when 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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- 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>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 31, 2026
Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@memleakd
Copy link
Copy Markdown
Contributor Author

memleakd commented Jun 2, 2026

I updated the docs to mention the prepared-query limitation.

About the API itself, I agree this is different from things like whereExists(), havingBetween(), transaction(), or retryable exceptions, where the feature clearly fills a missing capability. This is more of a small DX helper.

I’m happy to hear what others think and close this if undesired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants