PHP.GT

Working with conditions

where and having clauses can be written as plain strings, but the library becomes much more useful once we start using condition objects.

Condition objects help with:

  • logical grouping
  • repeated comparison patterns
  • short placeholder syntax
  • composing nested boolean logic without hand-writing every bracket

Plain strings are still valid

This is fine:

public function where():array {
	return [
		"deletedAt is null",
		"enabled = true",
	];
}

Multiple plain strings are joined with and.

AndCondition and OrCondition

For explicit logic, use:

  • GT\SqlBuilder\Condition\AndCondition
  • GT\SqlBuilder\Condition\OrCondition
// This query's where performs two logic conditions:
// 1) It ensures that deletedAt AND archivedAt is null
// ... and ...
// 2) It ensures that status is active OR pending
public function where():array {
	return [
		new AndCondition(
			"deletedAt is null",
			"archivedAt is null",
		),
		new OrCondition(
			"status = 'active'",
			"status = 'pending'",
		),
	];
}

Nested conditions are wrapped in brackets automatically where needed.

MultipleCondition

MultipleCondition is the reusable grouped-condition helper. It takes:

  1. a matcher object
  2. one or more conditions or plain strings

The matcher decides whether the parts are joined with and or or.

Available matchers:

  • GT\SqlBuilder\Condition\MatchAll
  • GT\SqlBuilder\Condition\MatchAny
use GT\SqlBuilder\Condition\Equals;
use GT\SqlBuilder\Condition\GreaterThan;
use GT\SqlBuilder\Condition\MatchAny;
use GT\SqlBuilder\Condition\MultipleCondition;

public function where():array {
	return [
		"deletedAt is null",
		new MultipleCondition(
			new MatchAny(),
			new Equals("status", "active"),
			new GreaterThan("score", 50),
		),
	];
}

This keeps the intent much clearer than manually juggling brackets inside a string.

Comparison helpers

The repository currently provides these comparison condition classes:

  • Equals
  • GreaterThan
  • LessThan
  • Between

Equals

use GT\SqlBuilder\Condition\Equals;

new Equals("score", 42);
new Equals("status", "active");
new Equals("deletedAt", null);

Rendered results:

  • score = 42
  • status = 'active'
  • deletedAt is null

Single quotes inside strings are escaped for SQL string literal syntax:

new Equals("surname", "O'Reilly");

renders as:

surname = 'O\'Reilly'

GreaterThan and LessThan

new GreaterThan("score", 100);
new LessThan("score", 10);

These also format booleans and DateTimeInterface values correctly for SQL text output.

Between

use GT\SqlBuilder\Condition\Between;

new Between("score", 10, 20);

renders:

score between 10 and 20

It also supports DateTimeInterface values.

Short placeholder syntax inside conditions

There are two shorthand forms for simple equality conditions:

  • ":id" becomes id = :id
  • "?id" becomes id = ?
public function where():array {
	return [
		":id",
		":name",
	];
}

renders as:

where
	id = :id
and
	name = :name

Do not mix named and indexed shorthand in one clause

The library explicitly rejects this:

public function where():array {
	return [
		":id",
		"?name",
	];
}

That throws MixedIndexedAndNamedParametersException.

The reason is simple: one clause should commit to either named placeholders or positional placeholders so the SQL stays predictable.

Prefer helper conditions when the value is data

If the right-hand side is user or application data, the helper classes often read better than assembling a literal string yourself:

new Equals("enabled", true);
new Between("createdAt", $from, $to);

If the expression is already a full SQL fragment, a plain string is usually clearer:

"coalesce(lastLogin, createdAt) > now() - interval 1 day"

Learn about Insert and update assignment syntax in the next section.