Class Connection
Represents a connection with a database server.
Property Summary
- 
        $_config protectedarray<string, mixed>Contains the configuration params for this connection. 
- 
        $_schemaCollection protectedCake\Database\Schema\CollectionInterface|nullThe schema collection object 
- 
        $_transactionLevel protectedintContains how many nested transactions have been started. 
- 
        $_transactionStarted protectedboolWhether a transaction is active in this connection. 
- 
        $_useSavePoints protectedboolWhether this connection can and should use savepoints for nested transactions. 
- 
        $cacher protectedPsr\SimpleCache\CacheInterface|nullCacher object instance. 
- 
        $nestedTransactionRollbackException protectedCake\Database\Exception\NestedTransactionRollbackException|nullNestedTransactionRollbackException object instance, will be stored if the rollback method is called in some nested transaction. 
- 
        $queryFactory protectedQueryFactory
- 
        $readDriver protectedCake\Database\Driver
- 
        $writeDriver protectedCake\Database\Driver
Method Summary
- 
          __construct() publicConstructor. 
- 
          __debugInfo() publicReturns an array that can be used to describe the internal state of this object. 
- 
          __destruct() publicDestructor 
- 
          begin() publicStarts a new transaction. 
- 
          cacheMetadata() publicEnables or disables metadata caching for this connection 
- 
          commit() publicCommits current transaction. 
- 
          config() publicGet the configuration data used to create the connection. 
- 
          configName() publicGet the configuration name for this connection. 
- 
          createDrivers() protectedCreates read and write drivers. 
- 
          createSavePoint() publicCreates a new save point for nested transactions. 
- 
          delete() publicExecutes a DELETE statement on the specified table. 
- 
          deleteQuery() publicCreate a new DeleteQuery instance for this connection. 
- 
          disableConstraints() publicRun an operation with constraints disabled. 
- 
          disableForeignKeys() publicRun driver specific SQL to disable foreign key checks. 
- 
          disableSavePoints() publicDisables the usage of savepoints. 
- 
          enableForeignKeys() publicRun driver specific SQL to enable foreign key checks. 
- 
          enableSavePoints() publicEnables/disables the usage of savepoints, enables only if driver the allows it. 
- 
          execute() publicExecutes a query using $params for interpolating values and $types as a hint for each those params. 
- 
          getCacher() publicGet a cacher. 
- 
          getDisconnectRetry() publicGet the retry wrapper object that is allows recovery from server disconnects while performing certain database actions, such as executing a query. 
- 
          getDriver() publicGets the driver instance. 
- 
          getSchemaCollection() publicGets a Schema\Collection object for this connection. 
- 
          inTransaction() publicChecks if a transaction is running. 
- 
          insert() publicExecutes an INSERT query on the specified table. 
- 
          insertQuery() publicCreate a new InsertQuery instance for this connection. 
- 
          isSavePointsEnabled() publicReturns whether this connection is using savepoints for nested transactions 
- 
          queryFactory() publicGet query factory instance. 
- 
          releaseSavePoint() publicReleases a save point by its name. 
- 
          role() publicReturns the connection role: read or write. 
- 
          rollback() publicRollback current transaction. 
- 
          rollbackSavepoint() publicRollback a save point by its name. 
- 
          run() publicExecutes the provided query after compiling it for the specific driver dialect and returns the executed Statement object. 
- 
          selectQuery() publicCreate a new SelectQuery instance for this connection. 
- 
          setCacher() publicSet a cacher. 
- 
          setSchemaCollection() publicSets a Schema\Collection object for this connection. 
- 
          transactional() publicExecutes a callback inside a transaction, if any exception occurs while executing the passed callback, the transaction will be rolled back If the result of the callback is false, the transaction will also be rolled back. Otherwise the transaction is committed after executing the callback.
- 
          update() publicExecutes an UPDATE statement on the specified table. 
- 
          updateQuery() publicCreate a new UpdateQuery instance for this connection. 
- 
          wasNestedTransactionRolledback() protectedReturns whether some nested transaction has been already rolled back. 
Method Detail
__construct() ¶ public
__construct(array<string, mixed> $config)Constructor.
Available options:
- driverSort name or FQCN for driver.
- logBoolean indicating whether to use query logging.
- nameConnection name.
- cacheMetaDataBoolean indicating whether metadata (datasource schemas) should be cached. If set to a string it will be used as the name of cache config to use.
- cacheKeyPrefixCustom prefix to use when generation cache keys. Defaults to connection name.
Parameters
- 
                array<string, mixed>$config
- Configuration array. 
__debugInfo() ¶ public
__debugInfo(): array<string, mixed>Returns an array that can be used to describe the internal state of this object.
Returns
array<string, mixed>cacheMetadata() ¶ public
cacheMetadata(string|bool $cache): voidEnables or disables metadata caching for this connection
Changing this setting will not modify existing schema collections objects.
Parameters
- 
                string|bool$cache
- Either boolean false to disable metadata caching, or true to use - _cake_model_or the name of the cache config to use.
Returns
voidcommit() ¶ public
commit(): boolCommits current transaction.
Returns
booltrue on success, false otherwise
config() ¶ public
config(): array<string, mixed>Get the configuration data used to create the connection.
Returns
array<string, mixed>configName() ¶ public
configName(): stringGet the configuration name for this connection.
Returns
stringcreateDrivers() ¶ protected
createDrivers(array<string, mixed> $config): array<string, \Cake\Database\Driver>Creates read and write drivers.
Parameters
- 
                array<string, mixed>$config
- Connection config 
Returns
array<string, \Cake\Database\Driver>createSavePoint() ¶ public
createSavePoint(string|int $name): voidCreates a new save point for nested transactions.
Parameters
- 
                string|int$name
- Save point name or id 
Returns
voiddelete() ¶ public
delete(string $table, array $conditions = [], array<string, string> $types = []): Cake\Database\StatementInterfaceExecutes a DELETE statement on the specified table.
Parameters
- 
                string$table
- the table to delete rows from 
- 
                array$conditions optional
- conditions to be set for delete statement 
- 
                array<string, string>$types optional
- list of associative array containing the types to be used for casting 
Returns
Cake\Database\StatementInterfacedeleteQuery() ¶ public
deleteQuery(string|null $table = null, array $conditions = [], array<string, string> $types = []): Cake\Database\Query\DeleteQueryCreate a new DeleteQuery instance for this connection.
Parameters
- 
                string|null$table optional
- The table to delete rows from. 
- 
                array$conditions optional
- Conditions to be set for the delete statement. 
- 
                array<string, string>$types optional
- Associative array containing the types to be used for casting. 
Returns
Cake\Database\Query\DeleteQuerydisableConstraints() ¶ public
disableConstraints(Closure $callback): mixedRun an operation with constraints disabled.
Constraints should be re-enabled after the callback succeeds/fails.
Example:
$connection->disableConstraints(function ($connection) {
  $connection->insertQuery('users')->execute();
});Parameters
- 
                Closure$callback
- Callback to run with constraints disabled 
Returns
mixedThe return value of the callback.
Throws
ExceptionWill re-throw any exception raised in $callback after rolling back the transaction.
disableForeignKeys() ¶ public
disableForeignKeys(): voidRun driver specific SQL to disable foreign key checks.
Returns
voiddisableSavePoints() ¶ public
disableSavePoints(): $thisDisables the usage of savepoints.
Returns
$thisenableForeignKeys() ¶ public
enableForeignKeys(): voidRun driver specific SQL to enable foreign key checks.
Returns
voidenableSavePoints() ¶ public
enableSavePoints(bool $enable = true): $thisEnables/disables the usage of savepoints, enables only if driver the allows it.
If you are trying to enable this feature, make sure you check
isSavePointsEnabled() to verify that savepoints were enabled successfully.
Parameters
- 
                bool$enable optional
- Whether save points should be used. 
Returns
$thisexecute() ¶ public
execute(string $sql, array $params = [], array $types = []): Cake\Database\StatementInterfaceExecutes a query using $params for interpolating values and $types as a hint for each those params.
Parameters
- 
                string$sql
- SQL to be executed and interpolated with $params 
- 
                array$params optional
- list or associative array of params to be interpolated in $sql as values 
- 
                array$types optional
- list or associative array of types to be used for casting values in query 
Returns
Cake\Database\StatementInterfaceexecuted statement
getCacher() ¶ public
getCacher(): Psr\SimpleCache\CacheInterfaceGet a cacher.
Returns
Psr\SimpleCache\CacheInterfacegetDisconnectRetry() ¶ public
getDisconnectRetry(): Cake\Core\Retry\CommandRetryGet 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\CommandRetryThe retry wrapper
getDriver() ¶ public
getDriver(string $role = self::ROLE_WRITE): Cake\Database\DriverGets the driver instance.
Parameters
- 
                string$role optional
- Connection role ('read' or 'write') 
Returns
Cake\Database\DrivergetSchemaCollection() ¶ public
getSchemaCollection(): Cake\Database\Schema\CollectionInterfaceGets a Schema\Collection object for this connection.
Returns
Cake\Database\Schema\CollectionInterfaceinTransaction() ¶ public
inTransaction(): boolChecks if a transaction is running.
Returns
boolTrue if a transaction is running else false.
insert() ¶ public
insert(string $table, array $values, array<string, string> $types = []): Cake\Database\StatementInterfaceExecutes an INSERT query on the specified table.
Parameters
- 
                string$table
- the table to insert values in 
- 
                array$values
- values to be inserted 
- 
                array<string, string>$types optional
- Array containing the types to be used for casting 
Returns
Cake\Database\StatementInterfaceinsertQuery() ¶ public
insertQuery(string|null $table = null, array $values = [], array<int|string, string> $types = []): Cake\Database\Query\InsertQueryCreate a new InsertQuery instance for this connection.
Parameters
- 
                string|null$table optional
- The table to insert rows into. 
- 
                array$values optional
- Associative array of column => value to be inserted. 
- 
                array<int|string, string>$types optional
- Associative array containing the types to be used for casting. 
Returns
Cake\Database\Query\InsertQueryisSavePointsEnabled() ¶ public
isSavePointsEnabled(): boolReturns whether this connection is using savepoints for nested transactions
Returns
booltrue if enabled, false otherwise
queryFactory() ¶ public
queryFactory(): Cake\Database\Query\QueryFactoryGet query factory instance.
Returns
Cake\Database\Query\QueryFactoryreleaseSavePoint() ¶ public
releaseSavePoint(string|int $name): voidReleases a save point by its name.
Parameters
- 
                string|int$name
- Save point name or id 
Returns
voidrollback() ¶ public
rollback(bool|null $toBeginning = null): boolRollback current transaction.
Parameters
- 
                bool|null$toBeginning optional
- Whether the transaction should be rolled back to the beginning of it. Defaults to false if using savepoints, or true if not. 
Returns
boolrollbackSavepoint() ¶ public
rollbackSavepoint(string|int $name): voidRollback a save point by its name.
Parameters
- 
                string|int$name
- Save point name or id 
Returns
voidrun() ¶ public
run(Cake\Database\Query $query): Cake\Database\StatementInterfaceExecutes 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\StatementInterfaceexecuted statement
selectQuery() ¶ public
selectQuery(Cake\Database\ExpressionInterface|Closure|array|string|float|int $fields = [], array|string $table = [], array<string, string> $types = []): Cake\Database\Query\SelectQuery<mixed>Create a new SelectQuery instance for this connection.
Parameters
- 
                Cake\Database\ExpressionInterface|Closure|array|string|float|int$fields optional
- Fields/columns list for the query. 
- 
                array|string$table optional
- The table or list of tables to query. 
- 
                array<string, string>$types optional
- Associative array containing the types to be used for casting. 
Returns
Cake\Database\Query\SelectQuery<mixed>setCacher() ¶ public
setCacher(Psr\SimpleCache\CacheInterface $cacher): $thisSet a cacher.
Parameters
- 
                Psr\SimpleCache\CacheInterface$cacher
Returns
$thissetSchemaCollection() ¶ public
setSchemaCollection(Cake\Database\Schema\CollectionInterface $collection): $thisSets a Schema\Collection object for this connection.
Parameters
- 
                Cake\Database\Schema\CollectionInterface$collection
- The schema collection object 
Returns
$thistransactional() ¶ public
transactional(Closure $callback): mixedExecutes a callback inside a transaction, if any exception occurs
while executing the passed callback, the transaction will be rolled back
If the result of the callback 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->deleteQuery('users')->execute();
});Parameters
- 
                Closure$callback
- The callback to execute within a transaction. 
Returns
mixedThe return value of the callback.
Throws
ExceptionWill re-throw any exception raised in $callback after rolling back the transaction.
update() ¶ public
update(string $table, array $values, array $conditions = [], array<string, string> $types = []): Cake\Database\StatementInterfaceExecutes an UPDATE statement on the specified table.
Parameters
- 
                string$table
- the table to update rows from 
- 
                array$values
- values to be updated 
- 
                array$conditions optional
- conditions to be set for update statement 
- 
                array<string, string>$types optional
- list of associative array containing the types to be used for casting 
Returns
Cake\Database\StatementInterfaceupdateQuery() ¶ public
updateQuery(Cake\Database\ExpressionInterface|string|null $table = null, array $values = [], array $conditions = [], array<string, string> $types = []): Cake\Database\Query\UpdateQueryCreate a new UpdateQuery instance for this connection.
Parameters
- 
                Cake\Database\ExpressionInterface|string|null$table optional
- The table to update rows of. 
- 
                array$values optional
- Values to be updated. 
- 
                array$conditions optional
- Conditions to be set for the update statement. 
- 
                array<string, string>$types optional
- Associative array containing the types to be used for casting. 
Returns
Cake\Database\Query\UpdateQuerywasNestedTransactionRolledback() ¶ protected
wasNestedTransactionRolledback(): boolReturns whether some nested transaction has been already rolled back.
Returns
boolProperty Detail
$_config ¶ protected
Contains the configuration params for this connection.
Type
array<string, mixed>$_schemaCollection ¶ protected
The schema collection object
Type
Cake\Database\Schema\CollectionInterface|null$_useSavePoints ¶ protected
Whether this connection can and should use savepoints for nested transactions.
Type
bool$nestedTransactionRollbackException ¶ protected
NestedTransactionRollbackException object instance, will be stored if the rollback method is called in some nested transaction.
Type
Cake\Database\Exception\NestedTransactionRollbackException|null