Documentation

QueryBuilder
in package

Table of Contents

Properties

$connection  : DriverBase
$debugger  : Debugger
$debugGlobalMode  : string|null
$debugOnceMode  : string|null
Debug mode for the query builder.
$fields  : string
$from  : string
$parameters  : Parameters
$placeholders  : array<int|string, mixed>
$queryString  : string
$queryType  : string
$rawQuery  : string
$result  : Result
$table  : string
$values  : array<string, mixed>
$where  : Where

Methods

__construct()  : mixed
Constructor for the QueryBuilder class.
debugOnce()  : self
Sets the debug mode for the query builder.
delete()  : self
Deletes records from the specified table based on the given conditions.
distinct()  : self
Sets the DISTINCT keyword in the query.
execute()  : bool|int
Executes a raw query and returns the result.
executeQuery()  : bool
Execute the SELECT query.
executeStatement()  : bool|int
Executes the prepared statement and returns the result.
fetch()  : mixed
Fetches the next row from the result set as an object or an array.
fetchAll()  : mixed
Fetches all rows from the database using the specified fetch mode.
from()  : $this
Set the table with optional JOIN clauses to select from.
getDebug()  : View
Get the debug view for the query builder.
getDebugMode()  : string|null
Get the debug mode value.
getFields()  : string
Get the fields to be selected in the query.
getFrom()  : string
Get the table name from the query builder.
getHTML()  : string
This function returns records from a SQL query as an HTML table.
getLastInsertId()  : string|false
Get the last insert ID.
getMaximumValue()  : mixed
Get the maximum value from a specific table field.
getParameters()  : Parameters
Get the parameters used in the query.
getQueryType()  : string
Get the query type.
getRawQuery()  : string
Get the raw SQL query string.
getResult()  : Result
Get the result of the query.
getTable()  : string
Get the table name associated with the query builder.
getValues()  : array<string, mixed>
Get the values of the query.
getWhere()  : Where
Get the Where object associated with this QueryBuilder.
groupBy()  : self
Set the GROUP BY clause for the query.
insert()  : self
Insert a new row into the specified table with the given values.
limit()  : self
Set the LIMIT clause for the query.
numRows()  : int|false
Returns the number of rows in the result set of the current query.
orderBy()  : self
Set the ORDER BY clause for the query.
parameters()  : self
Set the parameters for the query.
placeholders()  : self
Set the placeholders for the query.
query()  : self
select()  : self
Set the columns to select from the table.
setDebug()  : void
Sets the debug mode for the query builder.
transactionBegin()  : bool
Begin transaction processing
transactionCommit()  : bool
Commit and end transaction processing.
transactionRollback()  : bool
Roll back transaction processing.
update()  : self
Update records in the specified table.
where()  : self
Set the WHERE clause for the query.
clear()  : void
Clears the query builder by resetting all the properties.
dumpDebug()  : void
Dumps the debug information for the query builder.
getSqlForDelete()  : string
getSqlForInsert()  : string
Returns the SQL query string for an INSERT statement.
getSqlForSelect()  : string
Returns the SQL query for the SELECT statement.
getSqlForUpdate()  : string
Returns the SQL query string for an UPDATE statement.
getSqlStatement()  : string
Get the SQL statement.

Properties

$debugGlobalMode

private string|null $debugGlobalMode = null

$debugOnceMode

Debug mode for the query builder.

private string|null $debugOnceMode = null

debugGlobalMode is the global debug mode for the class. debugOnceMode is the debug mode for a single query.

If debugOnceMode is null, the global debug mode will be used. If debugOnceMode is set, it will override the global debug mode. Both can be set to null, 'off', 'on', or 'silent'.

If set to 'on', the class will display detailed error messages. If set to 'silent', the class will register all error messages, which can be retrieved using the getDebug() method.

$placeholders

private array<int|string, mixed> $placeholders = []

An indexed or associative array to store the placeholders used in the query.

$values

private array<string, mixed> $values

Associative array containing the fields and values

Methods

debugOnce()

Sets the debug mode for the query builder.

public debugOnce(string|null $mode) : self
Parameters
$mode : string|null

The debug mode to set.

Return values
self

The current instance of the QueryBuilder.

delete()

Deletes records from the specified table based on the given conditions.

public delete(string $table, array<int|string, mixed>|string $where) : self
Parameters
$table : string

The name of the table to delete records from.

$where : array<int|string, mixed>|string

The condition(s) to be used in the delete query.

Return values
self

The current instance of the QueryBuilder.

distinct()

Sets the DISTINCT keyword in the query.

public distinct() : self
Return values
self

execute()

Executes a raw query and returns the result.

public execute() : bool|int
Return values
bool|int

The result of the query execution.

executeQuery()

Execute the SELECT query.

public executeQuery() : bool
Return values
bool

executeStatement()

Executes the prepared statement and returns the result.

public executeStatement() : bool|int
Return values
bool|int

The number of affected rows or false if there was an error.

fetch()

Fetches the next row from the result set as an object or an array.

public fetch([int $fetch_parameters = PDO::FETCH_OBJ ]) : mixed
Parameters
$fetch_parameters : int = PDO::FETCH_OBJ

The fetch style to use. Default is \PDO::FETCH_OBJ.

Return values
mixed

The fetched row as an object or an array, depending on the fetch style.

fetchAll()

Fetches all rows from the database using the specified fetch mode.

public fetchAll([int $fetch_parameters = PDO::FETCH_OBJ ]) : mixed
Parameters
$fetch_parameters : int = PDO::FETCH_OBJ

The fetch mode to use. Defaults to \PDO::FETCH_OBJ.

Return values
mixed

An array of rows fetched from the database.

from()

Set the table with optional JOIN clauses to select from.

public from(string $from) : $this
Parameters
$from : string

The SQL FROM statement with optional joins. Example: 'table1 INNER JOIN table2 ON table1.id = table2.id'.

Return values
$this

The current instance of the QueryBuilder.

getDebug()

Get the debug view for the query builder.

public getDebug() : View
Return values
View

The debug view.

getDebugMode()

Get the debug mode value.

public getDebugMode() : string|null
Return values
string|null

The debug mode value.

getFields()

Get the fields to be selected in the query.

public getFields() : string

(for PHPUnit testing purposes only).

Return values
string

The fields to be selected.

getFrom()

Get the table name from the query builder.

public getFrom() : string

(for PHPUnit testing purposes only).

Return values
string

The From query.

getHTML()

This function returns records from a SQL query as an HTML table.

public getHTML(array<string|int, mixed> $records[, bool $showCount = true ][, string|null $tableAttr = null ][, string|null $thAttr = null ][, string|null $tdAttr = null ]) : string
Parameters
$records : array<string|int, mixed>

The records set - can be an array or array of objects according to the fetch parameters.

$showCount : bool = true

(Optional) true if you want to show the row count, false if you do not want to show the count.

$tableAttr : string|null = null

(Optional) Comma separated attributes for the table. e.g: 'class=my-class, style=color:#222'.

$thAttr : string|null = null

(Optional) Comma separated attributes for the header row. e.g: 'class=my-class, style=font-weight:bold'.

$tdAttr : string|null = null

(Optional) Comma separated attributes for the cells. e.g: 'class=my-class, style=font-weight:normal'.

Return values
string

HTML containing a table with all records listed.

getLastInsertId()

Get the last insert ID.

public getLastInsertId() : string|false
Return values
string|false

The last insert ID or false if there was an error.

getMaximumValue()

Get the maximum value from a specific table field.

public getMaximumValue(string $table, string $field) : mixed
Parameters
$table : string
$field : string
Return values
mixed

the field value or false if no value is found.

getParameters()

Get the parameters used in the query.

public getParameters() : Parameters

(for PHPUnit testing purposes only).

Return values
Parameters

The parameters used in the query.

getQueryType()

Get the query type.

public getQueryType() : string

(for PHPUnit testing purposes only).

Return values
string

The query type.

getRawQuery()

Get the raw SQL query string.

public getRawQuery() : string

(for PHPUnit testing purposes only).

Return values
string

The raw SQL query string.

getResult()

Get the result of the query.

public getResult() : Result

(for PHPUnit testing purposes only).

Return values
Result

The result of the query.

getTable()

Get the table name associated with the query builder.

public getTable() : string

(for PHPUnit testing purposes only).

Return values
string

The table name.

getValues()

Get the values of the query.

public getValues() : array<string, mixed>

(for PHPUnit testing purposes only).

Return values
array<string, mixed>

The values of the query.

getWhere()

Get the Where object associated with this QueryBuilder.

public getWhere() : Where

(for PHPUnit testing purposes only).

Return values
Where

The Where object.

groupBy()

Set the GROUP BY clause for the query.

public groupBy(string $groupBy) : self
Parameters
$groupBy : string

The GROUP BY clause.

Return values
self

The current instance of the QueryBuilder.

insert()

Insert a new row into the specified table with the given values.

public insert(string $table, array<string, mixed> $values) : self
Parameters
$table : string

The name of the table to insert into.

$values : array<string, mixed>

An associative array of column-value pairs to insert.

Return values
self

The QueryBuilder instance for method chaining.

limit()

Set the LIMIT clause for the query.

public limit(int|string $limit) : self
Parameters
$limit : int|string

The LIMIT clause.

Return values
self

The current instance of the QueryBuilder.

numRows()

Returns the number of rows in the result set of the current query.

public numRows() : int|false
Return values
int|false

The number of rows, or false on failure.

orderBy()

Set the ORDER BY clause for the query.

public orderBy(string $orderBy) : self
Parameters
$orderBy : string

The ORDER BY clause.

Return values
self

The current instance of the QueryBuilder.

parameters()

Set the parameters for the query.

public parameters(array<string, bool|int|string> $parameters) : self
Parameters
$parameters : array<string, bool|int|string>

An associative array of parameter names and values.

Return values
self

The QueryBuilder instance.

placeholders()

Set the placeholders for the query.

public placeholders(array<int|string, mixed> $placeholders) : self
Parameters
$placeholders : array<int|string, mixed>

An indexed or associative array to store the placeholders used in the query.

Return values
self

The QueryBuilder instance.

query()

public query(string $sql) : self
Parameters
$sql : string
Return values
self

select()

Set the columns to select from the table.

public select(string|array<string|int, string> $fields) : self
Parameters
$fields : string|array<string|int, string>

The columns to select. Can be a string or an array of strings. If it's an array, the columns will be joined with a comma.

Return values
self

The current instance of the QueryBuilder.

setDebug()

Sets the debug mode for the query builder.

public setDebug(string|null $mode) : void
Parameters
$mode : string|null

The debug mode to set.

transactionBegin()

Begin transaction processing

public transactionBegin() : bool
Tags
throws
Exception

If there is an error.

Return values
bool

Returns true if the transaction begins successfully, false otherwise.

transactionCommit()

Commit and end transaction processing.

public transactionCommit() : bool
Tags
throws
Exception

If there is an error.

Return values
bool

Returns true if the transaction is committed successfully, false otherwise.

transactionRollback()

Roll back transaction processing.

public transactionRollback() : bool
Tags
throws
Exception

If there is an error.

Return values
bool

Returns true if the transaction is rolled back successfully, false otherwise.

update()

Update records in the specified table.

public update(string $table, array<string, mixed> $values, array<int|string, mixed>|string $where) : self
Parameters
$table : string

The name of the table to update.

$values : array<string, mixed>

An associative array of column-value pairs to update.

$where : array<int|string, mixed>|string

The condition to filter the records to be updated. Can be a string or an array of conditions. If it's an array, the conditions will be joined with AND.

Return values
self

The current instance of the QueryBuilder.

where()

Set the WHERE clause for the query.

public where(array<int|string, mixed>|string $where) : self
Parameters
$where : array<int|string, mixed>|string

The WHERE clause. Can be a string or an array of conditions. If it's an array, the conditions will be joined with AND.

Return values
self

The current instance of the QueryBuilder.

clear()

Clears the query builder by resetting all the properties.

private clear() : void
Tags
access

private

dumpDebug()

Dumps the debug information for the query builder.

private dumpDebug(PDOStatement|null $pdoStatement, int|float|null $time[, string|null $errorMessage = null ]) : void
Parameters
$pdoStatement : PDOStatement|null

The PDOStatement object representing the query.

$time : int|float|null

The execution time of the query.

$errorMessage : string|null = null

The error message, if any.

getSqlForDelete()

private getSqlForDelete() : string
Return values
string

getSqlForInsert()

Returns the SQL query string for an INSERT statement.

private getSqlForInsert() : string
Return values
string

The SQL query string.

getSqlForSelect()

Returns the SQL query for the SELECT statement.

private getSqlForSelect() : string
Return values
string

The SQL query for the SELECT statement.

getSqlForUpdate()

Returns the SQL query string for an UPDATE statement.

private getSqlForUpdate() : string
Return values
string

The SQL query string.

getSqlStatement()

Get the SQL statement.

private getSqlStatement() : string
Return values
string

The SQL statement.


        
On this page

Search results