Represents a connection with a database server.
array<string, mixed>Contains the configuration params for this connection.
Cake\Database\DriverInterfaceDriver object, responsible for creating the real connection and provide specific SQL dialect.
boolWhether to log queries generated during this connection.
Psr\Log\LoggerInterface|nullLogger object instance.
Cake\Database\Schema\CollectionInterface|nullThe schema collection object
intContains how many nested transactions have been started.
boolWhether a transaction is active in this connection.
boolWhether this connection can and should use savepoints for nested transactions.
Psr\SimpleCache\CacheInterface|nullCacher object instance.
Cake\Database\Exception\NestedTransactionRollbackException|nullNestedTransactionRollbackException object instance, will be stored if the rollback method is called in some nested transaction.
Constructor.
Returns an array that can be used to describe the internal state of this object.
Destructor
Returns a new statement object that will log the activity for the passed original statement instance.
Starts a new transaction.
Enables or disables metadata caching for this connection
Converts a give value to a suitable database value based on type and return relevant internal statement type
Commits current transaction.
Compiles a Query object into a SQL string according to the dialect for this connection's driver
Get the configuration data used to create the connection.
Get the configuration name for this connection.
Connects to the configured database.
Creates driver from name, class name or instance.
Creates a new save point for nested transactions.
Executes a DELETE statement on the specified table.
Run an operation with constraints disabled.
Run driver specific SQL to disable foreign key checks.
Disable query logging
Disables the usage of savepoints.
Disconnects from database server.
Run driver specific SQL to enable foreign key checks.
Enable/disable query logging
Enables/disables the usage of savepoints, enables only if driver the allows it.
Executes a query using $params for interpolating values and $types as a hint for each those params.
Get a cacher.
Get the retry wrapper object that is allows recovery from server disconnects while performing certain database actions, such as executing a query.
Gets the driver instance.
Gets the logger object
Gets a Schema\Collection object for this connection.
Checks if a transaction is running.
Executes an INSERT query on the specified table.
Returns whether connection to database server was already established.
Check if query logging is enabled.
Returns whether this connection is using savepoints for nested transactions
Logs a Query string using the configured logger object.
Matches columns to corresponding types
Create a new Query instance for this connection.
Prepares a SQL statement to be executed.
Executes a SQL statement and returns the Statement object as result.
Quotes value to be used safely in database query.
Quotes a database identifier (a column name, table name, etc..) to be used safely in queries without the risk of using reserved words.
Releases a save point by its name.
Rollback current transaction.
Rollback a save point by its name.
Executes the provided query after compiling it for the specific driver dialect and returns the executed Statement object.
Set a cacher.
Sets the driver instance. If a string is passed it will be treated as a class name and will be instantiated.
Sets a logger
Sets a Schema\Collection object for this connection.
Returns whether the driver supports adding or dropping constraints to already created tables.
Checks if using quote() is supported.
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.
Executes an UPDATE statement on the specified table.
Returns whether some nested transaction has been already rolled back.
__construct(array<string, mixed> $config)
Constructor.
driver Sort name or FCQN for driver.log Boolean indicating whether to use query logging.name Connection name.cacheMetaData Boolean 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.cacheKeyPrefix Custom prefix to use when generation cache keys. Defaults to connection name.array<string, mixed> $config Configuration array.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>__destruct()
Destructor
Disconnects the driver to release the connection.
_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.
Cake\Database\StatementInterface $statement the instance to be decorated
Cake\Database\Log\LoggingStatementbegin(): void
Starts a new transaction.
voidcacheMetadata(string|bool $cache): void
Enables or disables metadata caching for this connection
Changing this setting will not modify existing schema collections objects.
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.
voidcast(mixed $value, Cake\Database\TypeInterface|string|int $type = 'string'): array
Converts a give value to a suitable database value based on type and return relevant internal statement type
mixed $value The value to cast
Cake\Database\TypeInterface|string|int $type optional The type name or type instance to use.
arraycommit(): bool
Commits current transaction.
boolcompileQuery(Cake\Database\Query $query, Cake\Database\ValueBinder $binder): string
Compiles a Query object into a SQL string according to the dialect for this connection's driver
Cake\Database\Query $query The query to be compiled
Cake\Database\ValueBinder $binder Value binder
stringconfig(): array<string, mixed>
Get the configuration data used to create the connection.
array<string, mixed>configName(): string
Get the configuration name for this connection.
stringconnect(): bool
Connects to the configured database.
boolCake\Database\Exception\MissingConnectionExceptioncreateDriver(Cake\Database\DriverInterface|string $name, array $config): Cake\Database\DriverInterface
Creates driver from name, class name or instance.
Cake\Database\DriverInterface|string $name Driver name, class name or instance.
array $config Driver config if $name is not an instance.
Cake\Database\DriverInterfaceCake\Database\Exception\MissingDriverExceptionCake\Database\Exception\MissingExtensionExceptioncreateSavePoint(string|int $name): void
Creates a new save point for nested transactions.
string|int $name Save point name or id
voiddelete(string $table, array $conditions = [], array<string> $types = []): Cake\Database\StatementInterface
Executes a DELETE statement on the specified table.
string $table the table to delete rows from
array $conditions optional conditions to be set for delete statement
array<string> $types optional list of associative array containing the types to be used for casting
Cake\Database\StatementInterfacedisableConstraints(callable $callback): mixed
Run an operation with constraints disabled.
Constraints should be re-enabled after the callback succeeds/fails.
$connection->disableConstraints(function ($connection) {
$connection->newQuery()->delete('users')->execute();
}); callable $callback mixeddisableForeignKeys(): void
Run driver specific SQL to disable foreign key checks.
voiddisableQueryLogging(): $this
Disable query logging
$thisdisableSavePoints(): $this
Disables the usage of savepoints.
$thisdisconnect(): void
Disconnects from database server.
voidenableForeignKeys(): void
Run driver specific SQL to enable foreign key checks.
voidenableQueryLogging(bool $enable = true): $this
Enable/disable query logging
bool $enable optional Enable/disable query logging
$thisenableSavePoints(bool $enable = true): $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 isSavePointsEnabled() to verify that savepoints were enabled successfully.
bool $enable optional Whether save points should be used.
$thisexecute(string $sql, array $params = [], array $types = []): Cake\Database\StatementInterface
Executes a query using $params for interpolating values and $types as a hint for each those params.
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
Cake\Database\StatementInterfacegetCacher(): Psr\SimpleCache\CacheInterface
Get a cacher.
Psr\SimpleCache\CacheInterfacegetDisconnectRetry(): 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.
Cake\Core\Retry\CommandRetrygetDriver(): Cake\Database\DriverInterface
Gets the driver instance.
Cake\Database\DriverInterfacegetLogger(): Psr\Log\LoggerInterface
Gets the logger object
Psr\Log\LoggerInterfacegetSchemaCollection(): Cake\Database\Schema\CollectionInterface
Gets a Schema\Collection object for this connection.
Cake\Database\Schema\CollectionInterfaceinTransaction(): bool
Checks if a transaction is running.
boolinsert(string $table, array $values, array<int|string, string> $types = []): Cake\Database\StatementInterface
Executes an INSERT query on the specified table.
string $table the table to insert values in
array $values values to be inserted
array<int|string, string> $types optional Array containing the types to be used for casting
Cake\Database\StatementInterfaceisConnected(): bool
Returns whether connection to database server was already established.
boolisQueryLoggingEnabled(): bool
Check if query logging is enabled.
boolisSavePointsEnabled(): bool
Returns whether this connection is using savepoints for nested transactions
boollog(string $sql): void
Logs a Query string using the configured logger object.
string $sql string to be logged
voidmatchTypes(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.
array $columns list or associative array of columns and parameters to be bound with types
array $types list or associative array of types
arraynewQuery(): Cake\Database\Query
Create a new Query instance for this connection.
Cake\Database\Queryprepare(Cake\Database\Query|string $query): Cake\Database\StatementInterface
Prepares a SQL statement to be executed.
Cake\Database\Query|string $query The SQL to convert into a prepared statement.
Cake\Database\StatementInterfacequery(string $sql): Cake\Database\StatementInterface
Executes a SQL statement and returns the Statement object as result.
string $sql The SQL query to execute.
Cake\Database\StatementInterfacequote(mixed $value, Cake\Database\TypeInterface|string|int $type = 'string'): string
Quotes value to be used safely in database query.
This uses PDO::quote() and requires supportsQuoting() to work.
mixed $value The value to quote.
Cake\Database\TypeInterface|string|int $type optional Type to be used for determining kind of quoting to perform
stringquoteIdentifier(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.
This does not require supportsQuoting() to work.
string $identifier The identifier to quote.
stringreleaseSavePoint(string|int $name): void
Releases a save point by its name.
string|int $name Save point name or id
voidrollback(bool|null $toBeginning = null): bool
Rollback current transaction.
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.
boolrollbackSavepoint(string|int $name): void
Rollback a save point by its name.
string|int $name Save point name or id
voidrun(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.
Cake\Database\Query $query The query to be executed
Cake\Database\StatementInterfacesetCacher(Psr\SimpleCache\CacheInterface $cacher): $this
Set a cacher.
Psr\SimpleCache\CacheInterface $cacher $thissetDriver(Cake\Database\DriverInterface|string $driver, array<string, mixed> $config = []): $this
Sets the driver instance. If a string is passed it will be treated as a class name and will be instantiated.
Cake\Database\DriverInterface|string $driver The driver instance to use.
array<string, mixed> $config optional Config for a new driver.
$thisCake\Database\Exception\MissingDriverExceptionCake\Database\Exception\MissingExtensionExceptionsetLogger(Psr\Log\LoggerInterface $logger): $this
Sets a logger
Psr\Log\LoggerInterface $logger Logger object
$thissetSchemaCollection(Cake\Database\Schema\CollectionInterface $collection): $this
Sets a Schema\Collection object for this connection.
Cake\Database\Schema\CollectionInterface $collection The schema collection object
$thissupportsDynamicConstraints(): bool
Returns whether the driver supports adding or dropping constraints to already created tables.
boolsupportsQuoting(): bool
Checks if using quote() is supported.
This is not required to use quoteIdentifier().
booltransactional(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.
$connection->transactional(function ($connection) {
$connection->newQuery()->delete('users')->execute();
}); callable $callback mixedupdate(string $table, array $values, array $conditions = [], array<string> $types = []): Cake\Database\StatementInterface
Executes an UPDATE statement on the specified table.
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> $types optional list of associative array containing the types to be used for casting
Cake\Database\StatementInterfacewasNestedTransactionRolledback(): bool
Returns whether some nested transaction has been already rolled back.
boolContains the configuration params for this connection.
array<string, mixed>Driver object, responsible for creating the real connection and provide specific SQL dialect.
Cake\Database\DriverInterfaceWhether to log queries generated during this connection.
boolLogger object instance.
Psr\Log\LoggerInterface|nullThe schema collection object
Cake\Database\Schema\CollectionInterface|nullContains how many nested transactions have been started.
intWhether a transaction is active in this connection.
boolWhether this connection can and should use savepoints for nested transactions.
boolCacher object instance.
Psr\SimpleCache\CacheInterface|nullNestedTransactionRollbackException object instance, will be stored if the rollback method is called in some nested transaction.
Cake\Database\Exception\NestedTransactionRollbackException|null
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Database.Connection.html