Skip to content

Conversation

@xJuvi
Copy link
Contributor

@xJuvi xJuvi commented Dec 29, 2025

With this PR, it is now possible to group WHERE conditions. This is necessary for more complex WHERE conditions so that RAW queries do not always have to be used.

Examples:

Query:

#Query:
#Beginning with normal condition
$db->whereInt('age', 18, '>=')
	->openWhereGroup('AND')
		->whereString('country', 'DE')
		->orWhereString('country', 'AT')
	->closeWhereGroup()
	->orWhereInt('is_admin', 1)
	->get('users');

#Result:
SELECT * FROM users WHERE age >= '18' AND (country = 'DE' OR country = 'AT') OR is_admin = '1'

#Query:
#Beginning with group
$db->openWhereGroup('AND')
		->whereString('country', 'DE')
		->orWhereString('country', 'AT')
		->whereInt('age', 18, '>=')
	->closeWhereGroup()
	->orWhereInt('is_admin', 1)
	->get('users');

#Result:
SELECT * FROM users WHERE (country = 'DE' OR country = 'AT' AND age >= '18') OR is_admin = '1'

#Query:
#Group inside another Group
$db->whereInt('age', 18, '>=')
	->openWhereGroup('OR')
		->whereString('country', 'DE')
		->orWhereString('country', 'AT')
		->openWhereGroup('AND')
			->whereString('city', 'Berlin')
			->whereString('number', '16')
		->closeWhereGroup()
	->closeWhereGroup()
	->orWhereInt('is_admin', 1)	
	->get('users');

#Result:
SELECT * FROM users WHERE age >= '18' OR (country = 'DE' OR country = 'AT' AND (city = 'Berlin' AND number = '16')) OR is_admin = '1'

I have already prepared unit tests for this. However, this requires a redesign of the library, as final classes cannot be mocked and the database connection is already established in the constructor. Here, it is necessary to introduce a parameter that prevents a database connection from being established.

Added error handling for unbalanced WHERE groups and improved condition evaluation logic.
@xJuvi xJuvi changed the title Enhance error handling for WHERE group conditions error handling and WHERE group conditions Dec 29, 2025
@decMuc
Copy link
Owner

decMuc commented Jan 9, 2026

Hey — sorry for the late reply. I was in the hospital :(

I actually like the idea 👍
But right now I only see the builder/parsing logic for the group tokens (__group_open / __group_close) and the related error handling.

What’s missing (unless I overlooked it) are the public APIs to actually use this feature:

openWhereGroup($operator = 'AND'|'OR')

closeWhereGroup()

Without those methods the feature is basically incomplete from the user side (the examples in the PR description wouldn’t work).

Could you please add these two methods (plus a short doc/example, and ideally 1–2 tests for nested groups / AND+OR)? Then I’m happy to take another look and merge it.

If you want, I can also suggest a minimal implementation that fits the current internal where-stack structure.

Add methods for open and close groups.
@xJuvi
Copy link
Contributor Author

xJuvi commented Jan 9, 2026

Hey @decMuc ,
I hope you're feeling better?

Due to a merge conflict, the public methods had actually disappeared... I added them back in.

You can find examples, including nested groups, in the description. I can adjust the documentation once the pull request has been merged.
I prepared the tests locally, but they are not possible without a real database because the constructor already wants to establish a connection. We either need to deploy a database in the GitHub Action or adjust the logic of the constructor. I have opened issue #15 for this. However, I am also happy to create the preparations for unit tests as a new PR in draft mode.

Please check my changes and merge them. My local tests are all successful.

@xJuvi
Copy link
Contributor Author

xJuvi commented Jan 18, 2026

Hey @decMuc
Any Updates?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants