Skip to content

feat: add Query Builder whereExists methods - #10185

Merged
paulbalandan merged 4 commits into
codeigniter4:4.8from
memleakd:feat/where-exists-query-builder
May 14, 2026
Merged

feat: add Query Builder whereExists methods#10185
paulbalandan merged 4 commits into
codeigniter4:4.8from
memleakd:feat/where-exists-query-builder

Conversation

@memleakd

Copy link
Copy Markdown
Contributor

Description

This PR proposes adding whereExists(), orWhereExists(), whereNotExists(), and orWhereNotExists() to the Query Builder.

EXISTS and NOT EXISTS conditions are common in real applications for filtering records by related data, checking membership/authorization relationships, and expressing “does a matching row exist?” without turning the query into a join.

$builder->whereExists(staticfunction (BaseBuilder$builder) {
$builder->select('1', false)
->from('orders')
->whereColumn('orders.user_id', 'users.id');
});

This keeps the condition inside the Query Builder flow instead of requiring users to write the EXISTS (...) clause manually. It also pairs naturally with the recently added whereColumn() support for correlated subqueries.

The API follows the existing Query Builder subquery style used by methods like whereIn(): it accepts a Closure or BaseBuilder instance. Raw SQL strings are intentionally not accepted; users who need fully manual SQL can still use where().

Changes

  • Added whereExists() and orWhereExists().
  • Added whereNotExists() and orWhereNotExists().
  • Added user guide docs, class reference entries, and changelog entry.
  • Added tests for Closure and builder subqueries, OR and NOT EXISTS variants, grouped conditions, bind handling, invalid inputs, and the existing same-builder 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

@github-actionsgithub-actionsBot added the 4.8 PRs that target the `4.8` branch. label May 10, 2026
@memleakd
memleakdforce-pushed the feat/where-exists-query-builder branch from 310086a to b7e16d1CompareMay 10, 2026 19:08
Comment threadsystem/Database/BaseBuilder.php Outdated
Comment threaduser_guide_src/source/database/query_builder.rst Outdated

@paulbalandanpaulbalandan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please rebase against latest 4.8.

@memleakd
memleakdforce-pushed the feat/where-exists-query-builder branch from 190d859 to 129fae7CompareMay 11, 2026 16:38
@paulbalandan

Copy link
Copy Markdown
Member

Sorry for the rector noise. It's being handled in #10181

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we add whereExists(), orWhereExists(), whereNotExists(), and orWhereNotExists() to the @method entries? https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Model.php#L43

@memleakd

Copy link
Copy Markdown
ContributorAuthor

Should we add whereExists(), orWhereExists(), whereNotExists(), and orWhereNotExists() to the @method entries? https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Model.php#L43

Yes, good point. Added the four @method entries to Model. Thank you!

@paulbalandan

Copy link
Copy Markdown
Member

A rebase then this can be merged. Thanks.

- Add whereExists() and whereNotExists() condition helpers
- Support Closure and BaseBuilder subqueries
- Document usage and raw SQL limitations
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd
memleakdforce-pushed the feat/where-exists-query-builder branch from c922eb2 to 47f3057CompareMay 14, 2026 20:21
@memleakd

Copy link
Copy Markdown
ContributorAuthor

A rebase then this can be merged. Thanks.

Done

@paulbalandan
paulbalandan merged commit 170b89a into codeigniter4:4.8May 14, 2026
56 checks passed
@memleakd

Copy link
Copy Markdown
ContributorAuthor

Thank you @paulbalandan@michalsn

@memleakd
memleakd deleted the feat/where-exists-query-builder branch May 14, 2026 20:59
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@memleakd@paulbalandan@michalsn