Properties

$_config

$_config : array

Contains the configuration params for this connection.

Type

array

$_driver

$_driver : \Cake\Database\Driver

Driver object, responsible for creating the real connection and provide specific SQL dialect.

Type

\Cake\Database\Driver

$_transactionLevel

$_transactionLevel : integer

Contains how many nested transactions have been started.

Type

integer

$_transactionStarted

$_transactionStarted : boolean

Whether a transaction is active in this connection.

Type

boolean

$_useSavePoints

$_useSavePoints : boolean

Whether this connection can and should use savepoints for nested transactions.

Type

boolean

$_logQueries

$_logQueries : boolean

Whether to log queries generated during this connection.

Type

boolean

$nestedTransactionRollbackException

$nestedTransactionRollbackException : \Cake\Database\Exception\NestedTransactionRollbackException|null

NestedTransactionRollbackException object instance, will be stored if the rollback method is called in some nested transaction.

Type

\Cake\Database\Exception\NestedTransactionRollbackException|null

Methods

cast()

cast(mixed  $value, \Cake\Database\Type|string  $type) : array

Converts a give value to a suitable database value based on type and return relevant internal statement type

Parameters

mixed $value

The value to cast

\Cake\Database\Type|string $type

The type name or type instance to use.

Returns

array —

list containing converted value and internal type

matchTypes()

matchTypes(array  $columns, array  $types) : array

Matches columns to corresponding types

Both $columns and $types should either be numeric based or string key based at the same time.

Parameters

array $columns

list or associative array of columns and parameters to be bound with types

array $types

list or associative array of types

Returns

array

__construct()

__construct(array  $config) 

Constructor.

Parameters

array $config

configuration for connecting to database

__destruct()

__destruct() 

Destructor

Disconnects the driver to release the connection.

config()

config() : array

Get the configuration data used to create the connection.

Returns

array

configName()

configName() : string

Get the configuration name for this connection.

Returns

string

setDriver()

setDriver(\Cake\Database\Driver|string  $driver, array  $config = array()) : $this

Sets the driver instance. If a string is passed it will be treated as a class name and will be instantiated.

Parameters

\Cake\Database\Driver|string $driver

The driver instance to use.

array $config

Config for a new driver.

Throws

\Cake\Database\Exception\MissingDriverException

When a driver class is missing.

\Cake\Database\Exception\MissingExtensionException

When a driver's PHP extension is missing.

Returns

$this

getDisconnectRetry()

getDisconnectRetry() : \Cake\Core\Retry\CommandRetry

Get the retry wrapper object that is allows recovery from server disconnects while performing certain database actions, such as executing a query.

Returns

\Cake\Core\Retry\CommandRetry

The retry wrapper

getDriver()

getDriver() : \Cake\Database\Driver

Gets the driver instance.

Returns

\Cake\Database\Driver

driver()

driver(\Cake\Database\Driver|string|null  $driver = null, array  $config = array()) : \Cake\Database\Driver

Sets the driver instance. If a string is passed it will be treated as a class name and will be instantiated.

If no params are passed it will return the current driver instance.

Parameters

\Cake\Database\Driver|string|null $driver

The driver instance to use.

array $config

Either config for a new driver or null.

Throws

\Cake\Database\Exception\MissingDriverException

When a driver class is missing.

\Cake\Database\Exception\MissingExtensionException

When a driver's PHP extension is missing.

Returns

\Cake\Database\Driver

connect()

connect() : boolean

Connects to the configured database.

Throws

\Cake\Database\Exception\MissingConnectionException

if credentials are invalid.

Returns

boolean —

true, if the connection was already established or the attempt was successful.

disconnect()

disconnect() : void

Disconnects from database server.

isConnected()

isConnected() : boolean

Returns whether connection to database server was already established.

Returns

boolean

execute()

execute(string  $query, array  $params = array(), array  $types = array()) : \Cake\Database\StatementInterface

Executes a query using $params for interpolating values and $types as a hint for each those params.

Parameters

string $query

SQL to be executed and interpolated with $params

array $params

list or associative array of params to be interpolated in $query as values

array $types

list or associative array of types to be used for casting values in query

Returns

\Cake\Database\StatementInterface

executed statement

compileQuery()

compileQuery(\Cake\Database\Query  $query, \Cake\Database\ValueBinder  $generator) : string

Compiles a Query object into a SQL string according to the dialect for this connection's driver

Parameters

\Cake\Database\Query $query

The query to be compiled

\Cake\Database\ValueBinder $generator

The placeholder generator to use

Returns

string

run()

run(\Cake\Database\Query  $query) : \Cake\Database\StatementInterface

Executes the provided query after compiling it for the specific driver dialect and returns the executed Statement object.

Parameters

\Cake\Database\Query $query

The query to be executed

Returns

\Cake\Database\StatementInterface

executed statement

query()

query(string  $sql) : \Cake\Database\StatementInterface

Executes a SQL statement and returns the Statement object as result.

Parameters

string $sql

The SQL query to execute.

Returns

\Cake\Database\StatementInterface

newQuery()

newQuery() : \Cake\Database\Query

Create a new Query instance for this connection.

Returns

\Cake\Database\Query

setSchemaCollection()

setSchemaCollection(\Cake\Database\Schema\Collection  $collection) : $this

Sets a Schema\Collection object for this connection.

Parameters

\Cake\Database\Schema\Collection $collection

The schema collection object

Returns

$this

getSchemaCollection()

getSchemaCollection() : \Cake\Database\Schema\Collection

Gets a Schema\Collection object for this connection.

Returns

\Cake\Database\Schema\Collection

schemaCollection()

schemaCollection(\Cake\Database\Schema\Collection|null  $collection = null) : \Cake\Database\Schema\Collection

Gets or sets a Schema\Collection object for this connection.

Parameters

\Cake\Database\Schema\Collection|null $collection

The schema collection object

Returns

\Cake\Database\Schema\Collection

insert()

insert(string  $table, array  $data, array  $types = array()) : \Cake\Database\StatementInterface

Executes an INSERT query on the specified table.

Parameters

string $table

the table to insert values in

array $data

values to be inserted

array $types

list of associative array containing the types to be used for casting

Returns

\Cake\Database\StatementInterface

update()

update(string  $table, array  $data, array  $conditions = array(), array  $types = array()) : \Cake\Database\StatementInterface

Executes an UPDATE statement on the specified table.

Parameters

string $table

the table to update rows from

array $data

values to be updated

array $conditions

conditions to be set for update statement

array $types

list of associative array containing the types to be used for casting

Returns

\Cake\Database\StatementInterface

delete()

delete(string  $table, array  $conditions = array(), array  $types = array()) : \Cake\Database\StatementInterface

Executes a DELETE statement on the specified table.

Parameters

string $table

the table to delete rows from

array $conditions

conditions to be set for delete statement

array $types

list of associative array containing the types to be used for casting

Returns

\Cake\Database\StatementInterface

begin()

begin() : void

Starts a new transaction.

commit()

commit() : boolean

Commits current transaction.

Returns

boolean —

true on success, false otherwise

rollback()

rollback(boolean|null  $toBeginning = null) : boolean

Rollback current transaction.

Parameters

boolean|null $toBeginning

Whether or not the transaction should be rolled back to the beginning of it. Defaults to false if using savepoints, or true if not.

Returns

boolean

enableSavePoints()

enableSavePoints(boolean  $enable) : $this

Enables/disables the usage of savepoints, enables only if driver the allows it.

If you are trying to enable this feature, make sure you check the return value of this function to verify it was enabled successfully.

Example:

$connection->enableSavePoints(true) Returns true if drivers supports save points, false otherwise $connection->enableSavePoints(false) Disables usage of savepoints and returns false

Parameters

boolean $enable

Whether or not save points should be used.

Returns

$this

disableSavePoints()

disableSavePoints() : $this

Disables the usage of savepoints.

Returns

$this

isSavePointsEnabled()

isSavePointsEnabled() : boolean

Returns whether this connection is using savepoints for nested transactions

Returns

boolean —

true if enabled, false otherwise

useSavePoints()

useSavePoints(boolean|null  $enable = null) : boolean

Returns whether this connection is using savepoints for nested transactions If a boolean is passed as argument it will enable/disable the usage of savepoints only if driver the allows it.

If you are trying to enable this feature, make sure you check the return value of this function to verify it was enabled successfully.

Example:

$connection->useSavePoints(true) Returns true if drivers supports save points, false otherwise $connection->useSavePoints(false) Disables usage of savepoints and returns false $connection->useSavePoints() Returns current status

Parameters

boolean|null $enable

Whether or not save points should be used.

Returns

boolean —

true if enabled, false otherwise

createSavePoint()

createSavePoint(string  $name) : void

Creates a new save point for nested transactions.

Parameters

string $name

The save point name.

releaseSavePoint()

releaseSavePoint(string  $name) : void

Releases a save point by its name.

Parameters

string $name

The save point name.

rollbackSavepoint()

rollbackSavepoint(string  $name) : void

Rollback a save point by its name.

Parameters

string $name

The save point name.

disableForeignKeys()

disableForeignKeys() : void

Run driver specific SQL to disable foreign key checks.

enableForeignKeys()

enableForeignKeys() : void

Run driver specific SQL to enable foreign key checks.

supportsDynamicConstraints()

supportsDynamicConstraints() : boolean

Returns whether the driver supports adding or dropping constraints to already created tables.

Returns

boolean —

true if driver supports dynamic constraints

transactional()

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.

Example:

$connection->transactional(function ($connection) {
  $connection->newQuery()->delete('users')->execute();
});

Parameters

callable $callback

Returns

mixed —

The return value of the callback.

disableConstraints()

disableConstraints(callable  $callback) : mixed

Run an operation with constraints disabled.

Example:

$connection->disableConstraints(function ($connection) {
  $connection->newQuery()->delete('users')->execute();
});

Parameters

callable $callback

Returns

mixed —

The return value of the callback.

inTransaction()

inTransaction() : boolean

Checks if a transaction is running.

Returns

boolean —

True if a transaction is running else false.

quote()

quote(mixed  $value, string|null  $type = null) : string

Quotes value to be used safely in database query.

Parameters

mixed $value

The value to quote.

string|null $type

Type to be used for determining kind of quoting to perform

Returns

string —

Quoted value

supportsQuoting()

supportsQuoting() : boolean

Checks if the driver supports quoting.

Returns

boolean

quoteIdentifier()

quoteIdentifier(string  $identifier) : string

Quotes a database identifier (a column name, table name, etc.

.) to be used safely in queries without the risk of using reserved words.

Parameters

string $identifier

The identifier to quote.

Returns

string

cacheMetadata()

cacheMetadata(boolean|string  $cache) : void

Enables or disables metadata caching for this connection

Changing this setting will not modify existing schema collections objects.

Parameters

boolean|string $cache

Either boolean false to disable metadata caching, or true to use _cake_model_ or the name of the cache config to use.

logQueries()

logQueries(boolean|null  $enable = null) : boolean

Enables or disables query logging for this connection.

Parameters

boolean|null $enable

whether to turn logging on or disable it. Use null to read current value.

Returns

boolean

enableQueryLogging()

enableQueryLogging(boolean  $value) : $this

Enable/disable query logging

Parameters

boolean $value

Enable/disable query logging

Returns

$this

disableQueryLogging()

disableQueryLogging() : $this

Disable query logging

Returns

$this

isQueryLoggingEnabled()

isQueryLoggingEnabled() : boolean

Check if query logging is enabled.

Returns

boolean

logger()

logger(object|null  $instance = null) : object

Sets the logger object instance. When called with no arguments it returns the currently setup logger instance.

Parameters

object|null $instance

logger object instance

Returns

object —

logger instance

setLogger()

setLogger(\Cake\Database\Log\QueryLogger  $logger) : $this

Sets a logger

Parameters

\Cake\Database\Log\QueryLogger $logger

Logger object

Returns

$this

log()

log(string  $sql) : void

Logs a Query string using the configured logger object.

Parameters

string $sql

string to be logged

__debugInfo()

__debugInfo() : array

Returns an array that can be used to describe the internal state of this object.

Returns

array

wasNestedTransactionRolledback()

wasNestedTransactionRolledback() : boolean

Returns whether some nested transaction has been already rolled back.

Returns

boolean

_newLogger()

_newLogger(\Cake\Database\StatementInterface  $statement) : \Cake\Database\Log\LoggingStatement

Returns a new statement object that will log the activity for the passed original statement instance.

Parameters

\Cake\Database\StatementInterface $statement

the instance to be decorated

Returns

\Cake\Database\Log\LoggingStatement