PHP Class Cake\Database\Connection

Inheritance: implements Cake\Datasource\ConnectionInterface, use trait Cake\Database\TypeConverterTrait
Datei anzeigen Open project: cakephp/cakephp Class Usage Examples

Protected Properties

Property Type Description
$_config array Contains the configuration params for this connection.
$_driver Cake\Database\Driver Driver object, responsible for creating the real connection and provide specific SQL dialect.
$_logQueries boolean Whether to log queries generated during this connection.
$_logger Cake\Database\Log\QueryLogger Logger object instance.
$_schemaCollection Cake\Database\Schema\Collection The schema collection object
$_transactionLevel integer Contains how many nested transactions have been started.
$_transactionStarted boolean Whether a transaction is active in this connection.
$_useSavePoints boolean Whether this connection can and should use savepoints for nested transactions.

Public Methods

Method Description
__construct ( array $config ) Constructor.
__debugInfo ( ) : array Returns an array that can be used to describe the internal state of this object.
__destruct ( ) Destructor
begin ( ) : void Starts a new transaction.
cacheMetadata ( boolean | string $cache ) : void Enables or disables metadata caching for this connection
commit ( ) : boolean Commits current transaction.
compileQuery ( Query $query, ValueBinder $generator ) : string Compiles a Query object into a SQL string according to the dialect for this connection's driver
config ( ) {@inheritDoc}
configName ( ) {@inheritDoc}
connect ( ) : boolean Connects to the configured database.
createSavePoint ( string $name ) : void Creates a new save point for nested transactions.
delete ( string $table, array $conditions = [], array $types = [] ) : Cake\Database\StatementInterface Executes a DELETE statement on the specified table.
disableConstraints ( callable $callback ) {@inheritDoc}
disableForeignKeys ( ) : void Run driver specific SQL to disable foreign key checks.
disconnect ( ) : void Disconnects from database server.
driver ( Cake\Database\Driver | string | null $driver = null, array $config = [] ) : Cake\Database\Driver Sets the driver instance. If a string is passed it will be treated as a class name and will be instantiated.
enableForeignKeys ( ) : void Run driver specific SQL to enable foreign key checks.
execute ( string $query, array $params = [], array $types = [] ) : Cake\Database\StatementInterface Executes a query using $params for interpolating values and $types as a hint for each those params.
inTransaction ( ) : boolean Checks if a transaction is running.
insert ( string $table, array $data, array $types = [] ) : Cake\Database\StatementInterface Executes an INSERT query on the specified table.
isConnected ( ) : boolean Returns whether connection to database server was already established.
log ( string $sql ) : void Logs a Query string using the configured logger object.
logQueries ( $enable = null ) {@inheritDoc}
logger ( $instance = null ) {@inheritDoc}
newQuery ( ) : Query Create a new Query instance for this connection.
prepare ( string | Query $sql ) : Cake\Database\StatementInterface Prepares a SQL statement to be executed.
query ( string $sql ) : Cake\Database\StatementInterface Executes a SQL statement and returns the Statement object as result.
quote ( mixed $value, string | null $type = null ) : string Quotes value to be used safely in database query.
quoteIdentifier ( string $identifier ) : string Quotes a database identifier (a column name, table name, etc.
releaseSavePoint ( string $name ) : void Releases a save point by its name.
rollback ( ) : boolean Rollback current transaction.
rollbackSavepoint ( string $name ) : void Rollback a save point by its name.
run ( Query $query ) : Cake\Database\StatementInterface Executes the provided query after compiling it for the specific driver dialect and returns the executed Statement object.
schemaCollection ( Cake\Database\Schema\Collection $collection = null ) : Cake\Database\Schema\Collection Gets or sets a Schema\Collection object for this connection.
supportsDynamicConstraints ( ) : boolean Returns whether the driver supports adding or dropping constraints to already created tables.
supportsQuoting ( ) : boolean Checks if the driver supports quoting.
transactional ( callable $callback ) {@inheritDoc}
update ( string $table, array $data, array $conditions = [], array $types = [] ) : Cake\Database\StatementInterface Executes an UPDATE statement on the specified table.
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.

Protected Methods

Method Description
_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.

Method Details

__construct() public method

Constructor.
public __construct ( array $config )
$config array configuration for connecting to database

__debugInfo() public method

Returns an array that can be used to describe the internal state of this object.
public __debugInfo ( ) : array
return array

__destruct() public method

Disconnects the driver to release the connection.
public __destruct ( )

_newLogger() protected method

Returns a new statement object that will log the activity for the passed original statement instance.
protected _newLogger ( Cake\Database\StatementInterface $statement ) : Cake\Database\Log\LoggingStatement
$statement Cake\Database\StatementInterface the instance to be decorated
return Cake\Database\Log\LoggingStatement

begin() public method

Starts a new transaction.
public begin ( ) : void
return void

cacheMetadata() public method

Changing this setting will not modify existing schema collections objects.
public cacheMetadata ( boolean | string $cache ) : void
$cache boolean | string Either boolean false to disable meta dataing caching, or true to use `_cake_model_` or the name of the cache config to use.
return void

commit() public method

Commits current transaction.
public commit ( ) : boolean
return boolean true on success, false otherwise

compileQuery() public method

Compiles a Query object into a SQL string according to the dialect for this connection's driver
public compileQuery ( Query $query, ValueBinder $generator ) : string
$query Query The query to be compiled
$generator ValueBinder The placeholder generator to use
return string

config() public method

{@inheritDoc}
public config ( )

configName() public method

{@inheritDoc}
public configName ( )

connect() public method

Connects to the configured database.
public connect ( ) : boolean
return boolean true on success or false if already connected.

createSavePoint() public method

Creates a new save point for nested transactions.
public createSavePoint ( string $name ) : void
$name string The save point name.
return void

delete() public method

Executes a DELETE statement on the specified table.
public delete ( string $table, array $conditions = [], array $types = [] ) : Cake\Database\StatementInterface
$table string the table to delete rows from
$conditions array conditions to be set for delete statement
$types array list of associative array containing the types to be used for casting
return Cake\Database\StatementInterface

disableConstraints() public method

### Example: $connection->disableConstraints(function ($connection) { $connection->newQuery()->delete('users')->execute(); });
public disableConstraints ( callable $callback )
$callback callable

disableForeignKeys() public method

Run driver specific SQL to disable foreign key checks.
public disableForeignKeys ( ) : void
return void

disconnect() public method

Disconnects from database server.
public disconnect ( ) : void
return void

driver() public method

If no params are passed it will return the current driver instance.
public driver ( Cake\Database\Driver | string | null $driver = null, array $config = [] ) : Cake\Database\Driver
$driver Cake\Database\Driver | string | null The driver instance to use.
$config array Either config for a new driver or null.
return Cake\Database\Driver

enableForeignKeys() public method

Run driver specific SQL to enable foreign key checks.
public enableForeignKeys ( ) : void
return void

execute() public method

Executes a query using $params for interpolating values and $types as a hint for each those params.
public execute ( string $query, array $params = [], array $types = [] ) : Cake\Database\StatementInterface
$query string SQL to be executed and interpolated with $params
$params array list or associative array of params to be interpolated in $query as values
$types array list or associative array of types to be used for casting values in query
return Cake\Database\StatementInterface executed statement

inTransaction() public method

Checks if a transaction is running.
public inTransaction ( ) : boolean
return boolean True if a transaction is running else false.

insert() public method

Executes an INSERT query on the specified table.
public insert ( string $table, array $data, array $types = [] ) : Cake\Database\StatementInterface
$table string the table to insert values in
$data array values to be inserted
$types array list of associative array containing the types to be used for casting
return Cake\Database\StatementInterface

isConnected() public method

Returns whether connection to database server was already established.
public isConnected ( ) : boolean
return boolean

log() public method

Logs a Query string using the configured logger object.
public log ( string $sql ) : void
$sql string string to be logged
return void

logQueries() public method

{@inheritDoc}
public logQueries ( $enable = null )

logger() public method

{@inheritDoc}
public logger ( $instance = null )

newQuery() public method

Create a new Query instance for this connection.
public newQuery ( ) : Query
return Query

prepare() public method

Prepares a SQL statement to be executed.
public prepare ( string | Query $sql ) : Cake\Database\StatementInterface
$sql string | Query The SQL to convert into a prepared statement.
return Cake\Database\StatementInterface

query() public method

Executes a SQL statement and returns the Statement object as result.
public query ( string $sql ) : Cake\Database\StatementInterface
$sql string The SQL query to execute.
return Cake\Database\StatementInterface

quote() public method

Quotes value to be used safely in database query.
public quote ( mixed $value, string | null $type = null ) : string
$value mixed The value to quote.
$type string | null Type to be used for determining kind of quoting to perform
return string Quoted value

quoteIdentifier() public method

.) to be used safely in queries without the risk of using reserved words.
public quoteIdentifier ( string $identifier ) : string
$identifier string The identifier to quote.
return string

releaseSavePoint() public method

Releases a save point by its name.
public releaseSavePoint ( string $name ) : void
$name string The save point name.
return void

rollback() public method

Rollback current transaction.
public rollback ( ) : boolean
return boolean

rollbackSavepoint() public method

Rollback a save point by its name.
public rollbackSavepoint ( string $name ) : void
$name string The save point name.
return void

run() public method

Executes the provided query after compiling it for the specific driver dialect and returns the executed Statement object.
public run ( Query $query ) : Cake\Database\StatementInterface
$query Query The query to be executed
return Cake\Database\StatementInterface executed statement

schemaCollection() public method

Gets or sets a Schema\Collection object for this connection.
public schemaCollection ( Cake\Database\Schema\Collection $collection = null ) : Cake\Database\Schema\Collection
$collection Cake\Database\Schema\Collection The schema collection object
return Cake\Database\Schema\Collection

supportsDynamicConstraints() public method

Returns whether the driver supports adding or dropping constraints to already created tables.
public supportsDynamicConstraints ( ) : boolean
return boolean true if driver supports dynamic constraints

supportsQuoting() public method

Checks if the driver supports quoting.
public supportsQuoting ( ) : boolean
return boolean

transactional() public method

### Example: $connection->transactional(function ($connection) { $connection->newQuery()->delete('users')->execute(); });
public transactional ( callable $callback )
$callback callable

update() public method

Executes an UPDATE statement on the specified table.
public update ( string $table, array $data, array $conditions = [], array $types = [] ) : Cake\Database\StatementInterface
$table string the table to update rows from
$data array values to be updated
$conditions array conditions to be set for update statement
$types array list of associative array containing the types to be used for casting
return Cake\Database\StatementInterface

useSavePoints() public method

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
public useSavePoints ( boolean | null $enable = null ) : boolean
$enable boolean | null Whether or not save points should be used.
return boolean true if enabled, false otherwise

Property Details

$_config protected_oe property

Contains the configuration params for this connection.
protected array $_config
return array

$_driver protected_oe property

Driver object, responsible for creating the real connection and provide specific SQL dialect.
protected Driver,Cake\Database $_driver
return Cake\Database\Driver

$_logQueries protected_oe property

Whether to log queries generated during this connection.
protected bool $_logQueries
return boolean

$_logger protected_oe property

Logger object instance.
protected QueryLogger,Cake\Database\Log $_logger
return Cake\Database\Log\QueryLogger

$_schemaCollection protected_oe property

The schema collection object
protected Collection,Cake\Database\Schema $_schemaCollection
return Cake\Database\Schema\Collection

$_transactionLevel protected_oe property

Contains how many nested transactions have been started.
protected int $_transactionLevel
return integer

$_transactionStarted protected_oe property

Whether a transaction is active in this connection.
protected bool $_transactionStarted
return boolean

$_useSavePoints protected_oe property

Whether this connection can and should use savepoints for nested transactions.
protected bool $_useSavePoints
return boolean