Working with builders
Builders provide the same general query shapes as the class-based API, but in a fluent style.
Available builders:
GT\SqlBuilder\SelectBuilderGT\SqlBuilder\InsertBuilderGT\SqlBuilder\UpdateBuilderGT\SqlBuilder\DeleteBuilder
There are currently no builder classes for the table-query types.
SelectBuilder
SelectBuilder supports:
select(string ...$args)from(string ...$args)innerJoin(string ...$args)crossJoin(string ...$args)leftJoin(string ...$args)leftOuterJoin(string ...$args)where(string ...$args)groupBy(string ...$args)having(string ...$args)orderBy(string ...$args)limit(int $limit)offset(int $offset)
use GT\SqlBuilder\SelectBuilder;
$query = new SelectBuilder();
$query->select(
"id",
"email",
"max(loginDateTime) as lastLogin",
)->from(
"user",
)->leftJoin(
"user_access on user.id = user_access.userId",
)->where(
"email like '%@example.com'",
"deletedAt is null",
)->groupBy(
"email",
)->having(
"lastLogin > '2020-01-01'",
)->orderBy(
"email",
)->limit(100);
InsertBuilder
InsertBuilder supports:
into(string ...$args)columns(string ...$args)values(string ...$args)set(array|string ...$args)
use GT\SqlBuilder\InsertBuilder;
$query = new InsertBuilder();
$query->into("student")
->set([
"id" => 123,
"name" => "'example'",
]);
That renders:
insert into
student
(
id,
name
)
values (
123,
'example'
)
We will look at set() in more detail in Insert and update assignment syntax.
UpdateBuilder
UpdateBuilder supports:
table(string ...$args)set(array|string ...$args)where(string ...$args)
use GT\SqlBuilder\UpdateBuilder;
$query = new UpdateBuilder();
$query->table("student")
->set(":processedAt")
->where(":id");
DeleteBuilder
DeleteBuilder supports:
from(string ...$args)where(string|Condition ...$args)
use GT\SqlBuilder\DeleteBuilder;
$query = new DeleteBuilder();
$query->from("student")
->where("softDelete is not null");
Builder calls return the builder itself
Every builder method returns the same builder instance, so chaining works naturally:
$query->select("id")->from("student")->where(":id");
getQuery() returns the corresponding query object
Each builder can also produce the query object it wraps:
$builder = new SelectBuilder();
$builder->select("id")->from("student");
$query = $builder->getQuery();
echo $query;
This is useful when another part of the code expects a SqlQuery object rather than the builder itself.
Invalid builder methods fail loudly
If you call a method that is not a supported query part, the builder throws an Error.
This is important because the builder methods are implemented through __call(). In other words, you get a fluent API, but only for the query parts the builder actually defines.
A practical limitation
Builders are good for expressing a query, not for building a mutating query object over many stages.
Remember:
- calling the same builder method again replaces the previous part
- builders do not automatically append to earlier
where()orselect()calls
So if you find yourself repeatedly revisiting the same builder in different parts of the code, that is often a sign the query should become a named query class instead.
For grouped logic and helper condition objects, continue with Working with conditions.