CallbackStatement
extends StatementDecorator
in package
Wraps a statement in a callback that allows row results to be modified when being fetched.
This is used by CakePHP to eagerly load association data.
Table of Contents
Properties
- $queryString : string
- $_callback : callable
- A callback function to be applied to results.
- $_driver : DriverInterface
- Reference to the driver object associated to this statement.
- $_hasExecuted : bool
- Whether this statement has already been executed
- $_statement : StatementInterface
- Statement instance implementation, such as PDOStatement or any other custom implementation.
Methods
- __construct() : mixed
- Constructor
- __get() : string|null
- Magic getter to return $queryString as read-only.
- bind() : void
- Binds a set of values to statement object with corresponding type.
- bindValue() : void
- Assign a value to a positional or named variable in prepared query. If using positional variables you need to start with index one, if using named params then just use the name in any order.
- cast() : array<string|int, mixed>
- Converts a give value to a suitable database value based on type and return relevant internal statement type
- closeCursor() : void
- Closes a cursor in the database, freeing up any resources and memory allocated to it. In most cases you don't need to call this method, as it is automatically called after fetching all results from the result set.
- columnCount() : int
- Returns the number of columns this statement's results will contain.
- count() : int
- Statements can be passed as argument for count() to return the number for affected rows from last execution.
- errorCode() : string|int
- Returns the error code for the last error that occurred when executing this statement.
- errorInfo() : array<string|int, mixed>
- Returns the error information for the last error that occurred when executing this statement.
- execute() : bool
- Executes the statement by sending the SQL query to the database. It can optionally take an array or arguments to be bound to the query variables. Please note that binding parameters from this method will not perform any custom type conversion as it would normally happen when calling `bindValue`.
- fetch() : array<string|int, mixed>|false
- Fetch a row from the statement.
- fetchAll() : array<string|int, mixed>|false
- Returns an array with all rows resulting from executing this statement.
- fetchAssoc() : array<string|int, mixed>
- Returns the next row in a result set as an associative array. Calling this function is the same as calling $statement->fetch(StatementDecorator::FETCH_TYPE_ASSOC). If no results are found an empty array is returned.
- fetchColumn() : mixed
- Returns the value of the result at position.
- getInnerStatement() : StatementInterface
- Returns the statement object that was decorated by this class.
- getIterator() : StatementInterface
- Statements are iterable as arrays, this method will return the iterator object for traversing all items in the result.
- lastInsertId() : string|int
- Returns the latest primary inserted using this statement.
- matchTypes() : array<string|int, mixed>
- Matches columns to corresponding types
- rowCount() : int
- Returns the number of rows affected by this SQL statement.
Properties
$queryString read-only
public
string
$queryString
$_callback
A callback function to be applied to results.
protected
callable
$_callback
$_driver
Reference to the driver object associated to this statement.
protected
DriverInterface
$_driver
$_hasExecuted
Whether this statement has already been executed
protected
bool
$_hasExecuted
= false
$_statement
Statement instance implementation, such as PDOStatement or any other custom implementation.
protected
StatementInterface
$_statement
Methods
__construct()
Constructor
public
__construct(StatementInterface $statement, DriverInterface $driver, callable $callback) : mixed
Parameters
- $statement : StatementInterface
-
The statement to decorate.
- $driver : DriverInterface
-
The driver instance used by the statement.
- $callback : callable
-
The callback to apply to results before they are returned.
__get()
Magic getter to return $queryString as read-only.
public
__get(string $property) : string|null
Parameters
- $property : string
-
internal property to get
Return values
string|nullbind()
Binds a set of values to statement object with corresponding type.
public
bind(array<string|int, mixed> $params, array<string|int, mixed> $types) : void
Parameters
- $params : array<string|int, mixed>
-
list of values to be bound
- $types : array<string|int, mixed>
-
list of types to be used, keys should match those in $params
bindValue()
Assign a value to a positional or named variable in prepared query. If using positional variables you need to start with index one, if using named params then just use the name in any order.
public
bindValue(string|int $column, mixed $value[, string|int|null $type = 'string' ]) : void
It is not allowed to combine positional and named variables in the same statement.
Examples:
$statement->bindValue(1, 'a title');
$statement->bindValue('active', true, 'boolean');
$statement->bindValue(5, new \DateTime(), 'date');
Parameters
- $column : string|int
-
name or param position to be bound
- $value : mixed
-
The value to bind to variable in query
- $type : string|int|null = 'string'
-
name of configured Type class
cast()
Converts a give value to a suitable database value based on type and return relevant internal statement type
public
cast(mixed $value[, TypeInterface|string|int $type = 'string' ]) : array<string|int, mixed>
Parameters
- $value : mixed
-
The value to cast
- $type : TypeInterface|string|int = 'string'
-
The type name or type instance to use.
Tags
Return values
array<string|int, mixed> —list containing converted value and internal type
closeCursor()
Closes a cursor in the database, freeing up any resources and memory allocated to it. In most cases you don't need to call this method, as it is automatically called after fetching all results from the result set.
public
closeCursor() : void
columnCount()
Returns the number of columns this statement's results will contain.
public
columnCount() : int
Example:
$statement = $connection->prepare('SELECT id, title from articles');
$statement->execute();
echo $statement->columnCount(); // outputs 2
Return values
intcount()
Statements can be passed as argument for count() to return the number for affected rows from last execution.
public
count() : int
Return values
interrorCode()
Returns the error code for the last error that occurred when executing this statement.
public
errorCode() : string|int
Return values
string|interrorInfo()
Returns the error information for the last error that occurred when executing this statement.
public
errorInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>execute()
Executes the statement by sending the SQL query to the database. It can optionally take an array or arguments to be bound to the query variables. Please note that binding parameters from this method will not perform any custom type conversion as it would normally happen when calling `bindValue`.
public
execute([array<string|int, mixed>|null $params = null ]) : bool
Parameters
- $params : array<string|int, mixed>|null = null
-
list of values to be bound to query
Return values
bool —true on success, false otherwise
fetch()
Fetch a row from the statement.
public
fetch([string|int $type = parent::FETCH_TYPE_NUM ]) : array<string|int, mixed>|false
The result will be processed by the callback when it is not false
.
Parameters
- $type : string|int = parent::FETCH_TYPE_NUM
-
Either 'num' or 'assoc' to indicate the result format you would like.
Return values
array<string|int, mixed>|falsefetchAll()
Returns an array with all rows resulting from executing this statement.
public
fetchAll([mixed $type = parent::FETCH_TYPE_NUM ]) : array<string|int, mixed>|false
Each row in the result will be processed by the callback when it is not `false.
Parameters
- $type : mixed = parent::FETCH_TYPE_NUM
-
num for fetching columns as positional keys or assoc for column names as keys
Return values
array<string|int, mixed>|false —List of all results from database for this statement. False on failure.
fetchAssoc()
Returns the next row in a result set as an associative array. Calling this function is the same as calling $statement->fetch(StatementDecorator::FETCH_TYPE_ASSOC). If no results are found an empty array is returned.
public
fetchAssoc() : array<string|int, mixed>
Return values
array<string|int, mixed>fetchColumn()
Returns the value of the result at position.
public
fetchColumn(int $position) : mixed
Parameters
- $position : int
-
The numeric position of the column to retrieve in the result
Return values
mixed —Returns the specific value of the column designated at $position
getInnerStatement()
Returns the statement object that was decorated by this class.
public
getInnerStatement() : StatementInterface
Return values
StatementInterfacegetIterator()
Statements are iterable as arrays, this method will return the iterator object for traversing all items in the result.
public
getIterator() : StatementInterface
Example:
$statement = $connection->prepare('SELECT id, title from articles');
foreach ($statement as $row) {
//do stuff
}
Tags
Attributes
- #[ReturnTypeWillChange]
Return values
StatementInterfacelastInsertId()
Returns the latest primary inserted using this statement.
public
lastInsertId([string|null $table = null ][, string|null $column = null ]) : string|int
Parameters
- $table : string|null = null
-
table name or sequence to get last insert value from
- $column : string|null = null
-
the name of the column representing the primary key
Return values
string|intmatchTypes()
Matches columns to corresponding types
public
matchTypes(array<string|int, mixed> $columns, array<string|int, mixed> $types) : array<string|int, mixed>
Both $columns and $types should either be numeric based or string key based at the same time.
Parameters
- $columns : array<string|int, mixed>
-
list or associative array of columns and parameters to be bound with types
- $types : array<string|int, mixed>
-
list or associative array of types
Return values
array<string|int, mixed>rowCount()
Returns the number of rows affected by this SQL statement.
public
rowCount() : int
Example:
$statement = $connection->prepare('SELECT id, title from articles');
$statement->execute();
print_r($statement->rowCount()); // will show 1