Skip to content

feat: add Query Builder insertGetID()#10260

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

feat: add Query Builder insertGetID()#10260
memleakd wants to merge 1 commit 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
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.

1 participant