Model
in package
Uses
CanBuildQuery, CanRetrieveModel
Base class for all models. Models should inherit from this.
It holds utility classes for working with the database
Table of Contents
- $booter : ZubZet
- $lastInsertId : int
- $z_db : Connection
- __construct() : mixed
- Creates a z_model instance.
- countResults() : int
- Returns the number of results in the last query.
- countTableEntries() : int
- Returns the number of datasets in a table.
- dbDelete() : DeleteQuery
- Create a new DeleteQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
- dbInsert() : InsertQuery
- Create a new InsertQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
- dbSelect() : SelectQuery
- Create a new SelectQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
- dbUpdate() : UpdateQuery
- Create a new UpdateQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
- exec() : Connection
- Executes a query as a prepared statement.
- 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. Table and fields are not escaped, so never put user input in them.
- getInsertId() : int|string|null
- Returns the last insert id. Only inserts of the slow-query log are ignored, other log writes on the shared connection overwrite it.
- getModel() : Model
- Returns a model, one shared instance per request; falls back to the framework models when not found
- getQueryBuilder() : mixed
- getResult() : null|bool|mysqli_result
- Returns the result of the last query.
- getTableWhere() : array<string|int, array<string|int, mixed>>
- Selects a full table or specified fields, filtered with an additional where statement. It returns the result as a two-dimensional array.
- heartbeat() : void
- Runs a very lightweight query to keep the connection alive.
- 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>|null
- Returns one line of the last query.
Properties
$booter
protected
ZubZet
$booter
Reference to the booter
$lastInsertId
protected
int
$lastInsertId
Unused, never written. getInsertId() reads the ID from the connection instead.
$z_db
protected
Connection
$z_db
Reference to the database proxy
Methods
__construct()
Creates a z_model instance.
public
__construct(Connection $z_db, ZubZet $booter) : mixed
This constructor should only be called from the booter. If you need a model, use $booter->getModel() instead.
Parameters
- $z_db : Connection
-
The database proxy class (Usually one lives in the booter)
- $booter : ZubZet
-
Booter object
Return values
mixed —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
dbDelete()
Create a new DeleteQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
public
dbDelete([string|null $table = null ][, array<string|int, mixed> $conditions = [] ][, array<string, string> $types = [] ]) : DeleteQuery
Parameters
- $table : string|null = null
-
The table to delete rows from.
- $conditions : array<string|int, mixed> = []
-
Conditions to be set for the delete statement.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
DeleteQuery —dbInsert()
Create a new InsertQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
public
dbInsert([string|null $table = null ][, array<string|int, mixed> $values = [] ][, array<int|string, string> $types = [] ]) : InsertQuery
Parameters
- $table : string|null = null
-
The table to insert rows into.
- $values : array<string|int, mixed> = []
-
Associative array of column => value to be inserted.
- $types : array<int|string, string> = []
-
Associative array containing the types to be used for casting.
Return values
InsertQuery —dbSelect()
Create a new SelectQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
public
dbSelect([ExpressionInterface|callable|array<string|int, mixed>|string $fields = [] ][, array<string|int, mixed>|string $table = [] ][, array<string, string> $types = [] ]) : SelectQuery
Parameters
- $fields : ExpressionInterface|callable|array<string|int, mixed>|string = []
-
fields to be added to the list.
- $table : array<string|int, mixed>|string = []
-
The table or list of tables to query.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
SelectQuery —dbUpdate()
Create a new UpdateQuery instance for the CakePHP\Database Connection. The query is only built, run it via exec()/execQuery() as the wrapped connection has no credentials for ->execute().
public
dbUpdate([ExpressionInterface|string|null $table = null ][, array<string|int, mixed> $values = [] ][, array<string|int, mixed> $conditions = [] ][, array<string, string> $types = [] ]) : UpdateQuery
Parameters
- $table : ExpressionInterface|string|null = null
-
The table to update rows of.
- $values : array<string|int, mixed> = []
-
Values to be updated.
- $conditions : array<string|int, mixed> = []
-
Conditions to be set for the update statement.
- $types : array<string, string> = []
-
Associative array containing the types to be used for casting.
Return values
UpdateQuery —exec()
Executes a query as a prepared statement.
public
exec(string|Query $query[, string $types = "" ][, mixed $params = null ]) : Connection
Parameters
- $query : string|Query
-
The query written as a prepared statement (with question marks), or a Query object.
- $types : string = ""
-
The types for the individual parameters (i for int, s for string...). Ignored for Query objects.
- $params : mixed = null
Return values
Connection —The Connection holding the result, for chaining
getFullTable()
Selects a full table or specified fields of it and returns the result as two-dimensional array. Table and fields are not escaped, so never put user input in them.
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 last insert id. Only inserts of the slow-query log are ignored, other log writes on the shared connection overwrite it.
public
getInsertId() : int|string|null
Return values
int|string|null —The ID of the dataset created in the last insert
getModel()
Returns a model, one shared instance per request; falls back to the framework models when not found
public
getModel(string $model[, string|null $dir = null ]) : Model
Parameters
- $model : string
-
Name of the model, sub-folders dot separated (e.g. "admin.User")
- $dir : string|null = null
-
Set this when the model is stored in a specific directory, needs a trailing slash
Tags
Return values
Model —The model
getQueryBuilder()
public
getQueryBuilder() : mixed
Return values
mixed —getResult()
Returns the result of the last query.
public
getResult() : null|bool|mysqli_result
Return values
null|bool|mysqli_result —Result of the last query
getTableWhere()
Selects a full table or specified fields, filtered with an additional where statement. It returns the result as a two-dimensional array.
public
getTableWhere(string $table, string $fields, string $where) : 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"...)
- $where : string
-
The where statement in the query, with "?" placeholders for values ("a = ? AND c = ?"...). Table, fields and where are not escaped, so never put user input in them.
Return values
array<string|int, array<string|int, mixed>> —Two-dimensional array with table data
heartbeat()
Runs a very lightweight query to keep the connection alive.
public
heartbeat([bool $waitForTimeout = true ]) : void
Parameters
- $waitForTimeout : bool = true
-
If set to true, only ping if no other query has been made within the timeout period
Tags
Return values
void —resultToArray()
Converts the result of the last query into an array and returns it.
public
resultToArray() : array<string|int, array<string|int, mixed>>
Tags
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>|null
Return values
array<string|int, mixed>|null —Line of the last result