Skip to content

feat: add Model existsById() helper#10262

Open
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/model-exists-by-id
Open

feat: add Model existsById() helper#10262
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/model-exists-by-id

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

Description

This PR proposes adding existsById() to CodeIgniter\Model.

Sometimes application code only needs to know whether a model row exists for a given primary key. Using find($id) works, but it also fetches and hydrates the row. This helper keeps that intent direct:

$userExists = $userModel->existsById($userId);

It uses the model's configured primary key, respects soft deletes, and can still be combined with query conditions when needed:

$activeUserExists = $userModel->where('active', 1)->existsById($userId);

So this does not introduce a new query path; it is just a small convenience around the existing model existence behavior.

Tests cover existing and missing IDs, soft deletes, constrained model queries, and confirm that this does not trigger find callbacks.

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 Model::existsById() for primary-key existence checks
- document soft-delete and query-builder composition behavior
- cover existing, missing, soft-deleted, constrained, and callback cases

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