Skip to content

feat: add Query Builder whereExists methods#10185

Open
memleakd wants to merge 1 commit intocodeigniter4:4.8from
memleakd:feat/where-exists-query-builder
Open

feat: add Query Builder whereExists methods#10185
memleakd wants to merge 1 commit intocodeigniter4: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(static function (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-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 10, 2026
- 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>
@memleakd memleakd force-pushed the feat/where-exists-query-builder branch from 310086a to b7e16d1 Compare May 10, 2026 19:08
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