Class Connection

Represents a connection with a database server.

Cake\Database\Connection implements Cake\Datasource\ConnectionInterface uses Cake\Database\TypeConverterTrait
Namespace: Cake\Database
Located at Database/Connection.php

Method Detail

__constructsource public

__construct( array $config )

Constructor.

Parameters

array $config
configuration for connecting to database

__debugInfosource public

__debugInfo( )

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

Returns

array
array

__destructsource public

__destruct( )

Destructor

Disconnects the driver to release the connection.

_newLoggersource protected

_newLogger( Cake\Database\StatementInterface $statement )

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
\Cake\Database\Log\LoggingStatement

beginsource public

begin( )

Starts a new transaction.

cacheMetadatasource public

cacheMetadata( boolean|string $cache )

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 meta dataing caching, or true to use _cake_model_ or the name of the cache config to use.

commitsource public

commit( )

Commits current transaction.

Returns

boolean
true on success, false otherwise

compileQuerysource public

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

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
string

configsource public

config( )

Get the configuration data used to create the connection.

Returns

array
array

Implementation of

Cake\Datasource\ConnectionInterface::config()

configNamesource public

configName( )

Get the configuration name for this connection.

Returns

string
string

Implementation of

Cake\Datasource\ConnectionInterface::configName()

connectsource public

connect( )

Connects to the configured database.

Returns

boolean
true on success or false if already connected.

Throws

Cake\Database\Exception\MissingConnectionException
if credentials are invalid

createSavePointsource public

createSavePoint( string $name )

Creates a new save point for nested transactions.

Parameters

string $name
The save point name.

deletesource public

delete( string $table , array $conditions [] , array $types [] )

Executes 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 $types optional []
list of associative array containing the types to be used for casting

Returns

Cake\Database\StatementInterface
\Cake\Database\StatementInterface

disableConstraintssource public

disableConstraints( callable $callback )

Run an operation with constraints disabled.

Example:

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

Parameters

callable $callback
$operation The callback to execute within a transaction.

Returns

mixed
The return value of the callback.

Throws

Exception
Will re-throw any exception raised in $callback after rolling back the transaction.

Implementation of

Cake\Datasource\ConnectionInterface::disableConstraints()

disableForeignKeyssource public

disableForeignKeys( )

Run driver specific SQL to disable foreign key checks.

disconnectsource public

disconnect( )

Disconnects from database server.

driversource public

driver( Cake\Database\Driver|string|null $driver null , array $config [] )

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 optional null
The driver instance to use.
array $config optional []
Either config for a new driver or null.

Returns

Cake\Database\Driver
\Cake\Database\Driver

Throws

Cake\Database\Exception\MissingDriverException
When a driver class is missing.
Cake\Database\Exception\MissingExtensionException
When a driver's PHP extension is missing.

enableForeignKeyssource public

enableForeignKeys( )

Run driver specific SQL to enable foreign key checks.

executesource public

execute( string $query , array $params [] , array $types [] )

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 optional []
list or associative array of params to be interpolated in $query as values
array $types optional []
list or associative array of types to be used for casting values in query

Returns

Cake\Database\StatementInterface
executed statement

inTransactionsource public

inTransaction( )

Checks if a transaction is running.

Returns

boolean
True if a transaction is running else false.

insertsource public

insert( string $table , array $data , array $types [] )

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 optional []
list of associative array containing the types to be used for casting

Returns

Cake\Database\StatementInterface
\Cake\Database\StatementInterface

isConnectedsource public

isConnected( )

Returns whether connection to database server was already established.

Returns

boolean
bool

logsource public

log( string $sql )

Logs a Query string using the configured logger object.

Parameters

string $sql
string to be logged

logQueriessource public

logQueries( boolean $enable null )

Enables or disables query logging for this connection.

Parameters

boolean $enable optional null
whether to turn logging on or disable it. Use null to read current value.

Returns

boolean
bool

Implementation of

Cake\Datasource\ConnectionInterface::logQueries()

loggersource public

logger( object $instance null )

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

Parameters

object $instance optional null
logger object instance

Returns

object
logger instance

Implementation of

Cake\Datasource\ConnectionInterface::logger()

newQuerysource public

newQuery( )

Create a new Query instance for this connection.

Returns

Cake\Database\Query
\Cake\Database\Query

preparesource public

prepare( string|Cake\Database\Query $sql )

Prepares a SQL statement to be executed.

Parameters

string|Cake\Database\Query $sql
The SQL to convert into a prepared statement.

Returns

Cake\Database\StatementInterface
\Cake\Database\StatementInterface

querysource public

query( string $sql )

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

Parameters

string $sql
The SQL query to execute.

Returns

Cake\Database\StatementInterface
\Cake\Database\StatementInterface

quotesource public

quote( mixed $value , string $type null )

Quotes value to be used safely in database query.

Parameters

mixed $value
The value to quote.
string $type optional null
Type to be used for determining kind of quoting to perform

Returns

string
Quoted value

quoteIdentifiersource public

quoteIdentifier( string $identifier )

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
string

releaseSavePointsource public

releaseSavePoint( string $name )

Releases a save point by its name.

Parameters

string $name
The save point name.

rollbacksource public

rollback( )

Rollback current transaction.

Returns

boolean
bool

rollbackSavepointsource public

rollbackSavepoint( string $name )

Rollback a save point by its name.

Parameters

string $name
The save point name.

runsource public

run( Cake\Database\Query $query )

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

schemaCollectionsource public

schemaCollection( Cake\Database\Schema\Collection $collection null )

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

Parameters

Cake\Database\Schema\Collection $collection optional null
The schema collection object

Returns

Cake\Database\Schema\Collection
\Cake\Database\Schema\Collection

supportsDynamicConstraintssource public

supportsDynamicConstraints( )

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

Returns

boolean
true if driver supports dynamic constraints

supportsQuotingsource public

supportsQuoting( )

Checks if the driver supports quoting.

Returns

boolean
bool

transactionalsource public

transactional( callable $callback )

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
$transaction The callback to execute within a transaction.

Returns

mixed
The return value of the callback.

Throws

Exception
Will re-throw any exception raised in $callback after rolling back the transaction.

Implementation of

Cake\Datasource\ConnectionInterface::transactional()

updatesource public

update( string $table , array $data , array $conditions [] , array $types [] )

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 optional []
conditions to be set for update statement
array $types optional []
list of associative array containing the types to be used for casting

Returns

Cake\Database\StatementInterface
\Cake\Database\StatementInterface

useSavePointssource public

useSavePoints( boolean|null $enable null )

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 optional null
Whether or not save points should be used.

Returns

boolean
true if enabled, false otherwise

Methods used from Cake\Database\TypeConverterTrait

castsource public

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

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

matchTypessource public

matchTypes( array $columns , array $types )

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
array

Properties summary

$_configsource

protected array

Contains the configuration params for this connection.

$_driversource

protected Cake\Database\Driver

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

$_logQueriessource

protected boolean

Whether to log queries generated during this connection.

false

$_loggersource

protected Cake\Database\Log\QueryLogger

Logger object instance.

null

$_schemaCollectionsource

protected Cake\Database\Schema\Collection

The schema collection object

$_transactionLevelsource

protected integer

Contains how many nested transactions have been started.

$_transactionStartedsource

protected boolean

Whether a transaction is active in this connection.

false

$_useSavePointssource

protected boolean

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

false

© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.Database.Connection.html