Interface ConnectionInterface
This interface defines the methods you can depend on in a connection.
Method Summary
-
config() public
Get the configuration data used to create the connection.
-
configName() public
Get the configuration name for this connection.
-
disableConstraints() public
Run an operation with constraints disabled.
-
disableQueryLogging() public
Disable query logging
-
enableQueryLogging() public
Enable/disable query logging
-
execute() public @method
Executes a query using
$params
for interpolating values and $types as a hint for each those params. {@see \Cake\Database\Connnection::execute()} -
getCacher() public
Get a cacher.
-
getDriver() public @method
Gets the driver instance. {@see \Cake\Database\Connnection::getDriver()}
-
getLogger() public
Gets the current logger object.
-
getSchemaCollection() public @method
Gets a Schema\Collection object for this connection. {@see \Cake\Database\Connnection::getSchemaCollection()}
-
isQueryLoggingEnabled() public
Check if query logging is enabled.
-
newQuery() public @method
Create a new Query instance for this connection. {@see \Cake\Database\Connnection::newQuery()}
-
prepare() public @method
Prepares a SQL statement to be executed. {@see \Cake\Database\Connnection::prepare()}
-
query() public @method
Executes a SQL statement and returns the Statement object as result. {@see \Cake\Database\Connnection::query()}
-
setCacher() public
Set a cacher.
-
setLogger() public @method
Set the current logger. {@see \Cake\Database\Connnection::setLogger()}
-
supportsDynamicConstraints() public @method
Returns whether the driver supports adding or dropping constraints to already created tables. {@see \Cake\Database\Connnection::supportsDynamicConstraints()}
-
transactional() public
Executes a callable function inside a transaction, if any exception occurs while executing the passed callable, the transaction will be rolled back If the result of the callable function is
false
, the transaction will also be rolled back. Otherwise, the transaction is committed after executing the callback.
Method Detail
config() ¶ public
config(): array<string, mixed>
Get the configuration data used to create the connection.
Returns
array<string, mixed>
configName() ¶ public
configName(): string
Get the configuration name for this connection.
Returns
string
disableConstraints() ¶ public
disableConstraints(callable $callback): mixed
Run an operation with constraints disabled.
Constraints should be re-enabled after the callback succeeds/fails.
Example:
$connection->disableConstraints(function ($connection) {
$connection->newQuery()->delete('users')->execute();
});
Parameters
-
callable
$callback The callback to execute within a transaction.
Returns
mixed
Throws
Exception
Will re-throw any exception raised in $callback after rolling back the transaction.
enableQueryLogging() ¶ public
enableQueryLogging(bool $enable = true): $this
Enable/disable query logging
Parameters
-
bool
$enable optional Enable/disable query logging
Returns
$this
execute() ¶ public @method
execute(mixed $query, mixed $params = [], array $types = []): Cake\Database\StatementInterface
Executes a query using
$params
for interpolating values and $types as a hint for each those params.
{@see \Cake\Database\Connnection::execute()}
Parameters
-
$query
-
$params optional
-
array
$types optional
Returns
Cake\Database\StatementInterface
getCacher() ¶ public
getCacher(): Psr\SimpleCache\CacheInterface
Get a cacher.
Returns
Psr\SimpleCache\CacheInterface
getDriver() ¶ public @method
getDriver(): object
Gets the driver instance. {@see \Cake\Database\Connnection::getDriver()}
Returns
object
getLogger() ¶ public
getLogger(): Psr\Log\LoggerInterface
Gets the current logger object.
Returns
Psr\Log\LoggerInterface
getSchemaCollection() ¶ public @method
getSchemaCollection(): Cake\Database\Schema\Collection
Gets a Schema\Collection object for this connection. {@see \Cake\Database\Connnection::getSchemaCollection()}
Returns
Cake\Database\Schema\Collection
isQueryLoggingEnabled() ¶ public
isQueryLoggingEnabled(): bool
Check if query logging is enabled.
Returns
bool
newQuery() ¶ public @method
newQuery(): Cake\Database\Query
Create a new Query instance for this connection. {@see \Cake\Database\Connnection::newQuery()}
Returns
Cake\Database\Query
prepare() ¶ public @method
prepare(mixed $sql): Cake\Database\StatementInterface
Prepares a SQL statement to be executed. {@see \Cake\Database\Connnection::prepare()}
Parameters
-
$sql
Returns
Cake\Database\StatementInterface
query() ¶ public @method
query(string $sql): Cake\Database\StatementInterface
Executes a SQL statement and returns the Statement object as result. {@see \Cake\Database\Connnection::query()}
Parameters
-
string
$sql
Returns
Cake\Database\StatementInterface
setCacher() ¶ public
setCacher(Psr\SimpleCache\CacheInterface $cacher): $this
Set a cacher.
Parameters
-
Psr\SimpleCache\CacheInterface
$cacher Cacher object
Returns
$this
setLogger() ¶ public @method
setLogger(LoggerInterface $logger): void
Set the current logger. {@see \Cake\Database\Connnection::setLogger()}
Parameters
-
LoggerInterface
$logger
Returns
void
supportsDynamicConstraints() ¶ public @method
supportsDynamicConstraints(): bool
Returns whether the driver supports adding or dropping constraints to already created tables. {@see \Cake\Database\Connnection::supportsDynamicConstraints()}
Returns
bool
transactional() ¶ public
transactional(callable $callback): mixed
Executes a callable function inside a transaction, if any exception occurs
while executing the passed callable, the transaction will be rolled back
If the result of the callable function is false
, the transaction will
also be rolled back. Otherwise, the transaction is committed after executing
the callback.
The callback will receive the connection instance as its first argument.
Example:
$connection->transactional(function ($connection) {
$connection->newQuery()->delete('users')->execute();
});
Parameters
-
callable
$callback The callback to execute within a transaction.
Returns
mixed
Throws
Exception
Will re-throw any exception raised in $callback after rolling back the transaction.