Connection
in package
implements
Checkpointable
Uses
Interaction, CanCheckpoint
Interfaces, Classes and Traits
- Checkpointable
- Contract for classes that want checkpoint/restore of their state.
Table of Contents
- $booter : ZubZet
- $callingModel : Model|null
- $connectTimeout : int
- $insertId : int|string|null
- $lastConnect : int
- $lastHeartbeat : int
- $queryBuilderConnection : Connection
- $result : null|bool|mysqli_result
- $conn : mysqli
- $database : string|null
- $host : string|null
- $password : string|null
- $stmt : mysqli_stmt
- $user : string|null
- __construct() : mixed
- __destruct() : mixed
- Closes the database connection on exit
- assertConnection() : mixed
- checkIfExists() : bool
- Checks if a value exists in a table.
- checkIfUnique() : bool
- Checks if a value is already in a table. Can also ignore a dataset.
- checkpointCurrentState() : Checkpoint
- Snapshot selected properties into a Checkpoint for later restore()
- countResults() : int
- Returns the number of results in the last query
- countTableEntries() : int
- Returns the number of datasets in a table
- disconnect() : void
- Disconnect from the database
- exec() : Connection
- Executes a query as prepared statement
- execQuery() : Connection
- Executes a CakePHP Query object using ZubZet`s own value binder to extract the SQL and bindings, then executing it as a prepared statement
- executeMultiQuery() : bool
- getDatabaseConnection() : mysqli
- getFullTable() : array<string|int, array<string|int, mixed>>
- Selects a full table or specified fields of it and returns the result as two dimensional array
- getInsertId() : int|string|null
- Returns the id of the last inserted element
- getTableWhere() : array<string|int, array<string|int, mixed>>
- Selects a full table of specified fields of it filtered with an additional where statement. It returns the result as two dimensional array
- heartbeat() : bool
- Run a very lightweight query to keep the connection alive.
- mergeAsGroup() : array<string|int, array<string|int, mixed>>
- Converts the result of the last query into a grouped array
- resultToArray() : array<string|int, array<string|int, mixed>>
- Converts the result of the last query into an array and returns it
- resultToLine() : array<string|int, mixed>
- Returns one line of the last query
- switchUser() : void
- checkpointablePropertyNames() : array<string|int, string>
- connect() : mixed
- pingConnection() : bool
- Sends a lightweight query to check whether an already-established connection is still responding. Assumes a connection has been opened.
Properties
$booter
public
ZubZet
$booter
$callingModel
public
Model|null
$callingModel
= null
A reference to the model that is currently handling the query, if any
$connectTimeout
public
int
$connectTimeout
$insertId
public
int|string|null
$insertId
Last insert id
$lastConnect
public
int
$lastConnect
$lastHeartbeat
public
int
$lastHeartbeat
$queryBuilderConnection
public
Connection
$queryBuilderConnection
$result
public
null|bool|mysqli_result
$result
Result of the last query
$conn
private
mysqli
$conn
$database
private
string|null
$database
$host
private
string|null
$host
$password
private
string|null
$password
$stmt
private
mysqli_stmt
$stmt
$user
private
string|null
$user
Methods
__construct()
public
__construct() : mixed
Return values
mixed —__destruct()
Closes the database connection on exit
public
__destruct() : mixed
Return values
mixed —assertConnection()
public
assertConnection() : mixed
Return values
mixed —checkIfExists()
Checks if a value exists in a table.
public
checkIfExists(string $table, string $field, mixed $value) : bool
Table and field names are inserted unescaped. Check your input.
Parameters
- $table : string
-
Name of the table to check in
- $field : string
-
Name of the field in that a value should exist
- $value : mixed
-
Value to check for
Return values
bool —True when exists
checkIfUnique()
Checks if a value is already in a table. Can also ignore a dataset.
public
checkIfUnique(string $table, string $field, mixed $value[, string $ignoreField = null ][, string $ignoreValue = null ]) : bool
Table and field names are inserted unescaped. Check your input.
Parameters
- $table : string
-
Name of the table to check in
- $field : string
-
Field to check in
- $value : mixed
-
Value to check for
- $ignoreField : string = null
-
field of a dataset to ignore
- $ignoreValue : string = null
-
value of the in the argument before defined field of the dataset to ignore
Return values
bool —True when not exists
checkpointCurrentState()
Snapshot selected properties into a Checkpoint for later restore()
public
checkpointCurrentState([array<string|int, mixed>|null $properties = null ][, string|null $attributeClass = null ]) : Checkpoint
Parameters
- $properties : array<string|int, mixed>|null = null
- $attributeClass : string|null = null
Return values
Checkpoint —countResults()
Returns the number of results in the last query
public
countResults() : int
Return values
int —Number of results in the last query
countTableEntries()
Returns the number of datasets in a table
public
countTableEntries(string $table) : int
Parameters
- $table : string
-
Name of the table in the database
Return values
int —Number of datasets in the specified table
disconnect()
Disconnect from the database
public
disconnect() : void
Return values
void —exec()
Executes a query as prepared statement
public
exec(string $query) : Connection
Parameters
- $query : string
-
Query written as prepared statement (that thing with the question marks as placeholders)
Return values
Connection —Returning this for chaining
execQuery()
Executes a CakePHP Query object using ZubZet`s own value binder to extract the SQL and bindings, then executing it as a prepared statement
public
execQuery(Query $query) : Connection
Parameters
- $query : Query
-
The CakePHP Query object to execute
Return values
Connection —Returning this for chaining
executeMultiQuery()
public
executeMultiQuery(string $query[, bool $throwOnFailure = true ]) : bool
Parameters
- $query : string
- $throwOnFailure : bool = true
Return values
bool —getDatabaseConnection()
public
getDatabaseConnection() : mysqli
Return values
mysqli —getFullTable()
Selects a full table or specified fields of it and returns the result as two dimensional array
public
getFullTable(string $table[, string $fields = "*" ]) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string = "*"
-
Fields to select. Formatted as in an SQL query ("*", "a, b, c"...)
Return values
array<string|int, array<string|int, mixed>> —A two dimensional array with the results of the select statement
getInsertId()
Returns the id of the last inserted element
public
getInsertId() : int|string|null
Return values
int|string|null —Id of the last inserted element
getTableWhere()
Selects a full table of specified fields of it filtered with an additional where statement. It returns the result as two dimensional array
public
getTableWhere(string $table[, string $fields = "*" ][, string $where = "" ][, string $types = "" ][, array<string|int, mixed> $values = [] ]) : array<string|int, array<string|int, mixed>>
Parameters
- $table : string
-
Name of the table in the database
- $fields : string = "*"
-
Fields to select. Formatted as in a SQL query ("*", "a, b, c"...)
- $where : string = ""
-
The where statement in the query. Formatted as in a SQL query (a = 4 AND c = 4...);
- $types : string = ""
-
String with the types. Conform to prepared statements ("ssis")
- $values : array<string|int, mixed> = []
-
The values to insert in the prepared statement
Return values
array<string|int, array<string|int, mixed>> —two dimensional array with table data
heartbeat()
Run a very lightweight query to keep the connection alive.
public
heartbeat([bool $waitForTimeout = true ][, int $timeoutBuffer = 30 ]) : bool
Parameters
- $waitForTimeout : bool = true
-
Only ping if no heartbeat happened within the timeout window
- $timeoutBuffer : int = 30
-
Seconds subtracted from the timeout before a ping is forced
Tags
Return values
bool —Whether a live connection responded. False when no connection has been opened yet (lazy loading) or the ping failed.
mergeAsGroup()
Converts the result of the last query into a grouped array
public
mergeAsGroup(string $groupBy[, string $subElement = null ]) : array<string|int, array<string|int, mixed>>
Parameters
- $groupBy : string
-
The field, by which the array is grouped by
- $subElement : string = null
-
If set, the only a sub element of the grouped element is returned
Return values
array<string|int, array<string|int, mixed>> —Results of the last query as two dimensional array with the index as thr groupBy value
resultToArray()
Converts the result of the last query into an array and returns it
public
resultToArray([mixed $out = [] ]) : array<string|int, array<string|int, mixed>>
Parameters
- $out : mixed = []
Return values
array<string|int, array<string|int, mixed>> —Results of the last query as two dimensional array
resultToLine()
Returns one line of the last query
public
resultToLine() : array<string|int, mixed>
Return values
array<string|int, mixed> —Line of the last result
switchUser()
public
switchUser(string $user, string $password) : void
Parameters
- $user : string
- $password : string
Return values
void —checkpointablePropertyNames()
protected
checkpointablePropertyNames([string|null $attributeClass = IncludeInCheckpoint::class ]) : array<string|int, string>
Parameters
- $attributeClass : string|null = IncludeInCheckpoint::class
Return values
array<string|int, string> —Filtered by $attributeClass when given, else all non-static instance properties.
connect()
private
connect() : mixed
Return values
mixed —pingConnection()
Sends a lightweight query to check whether an already-established connection is still responding. Assumes a connection has been opened.
private
pingConnection() : bool
Return values
bool —True if the server answered, false if the connection is dead