CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Connection
  • Driver
  • FieldTypeConverter
  • FunctionsBuilder
  • Query
  • SchemaCache
  • Type
  • TypeMap

Interfaces

  • DriverInterface
  • ExpressionInterface
  • StatementInterface
  • TypedResultInterface
  • TypeInterface

Traits

  • SqlDialectTrait
  • TypeConverterTrait
  • TypedResultTrait
  • TypeMapTrait

Exceptions

  • Exception

Class Connection

Represents a connection with a database server.

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

Properties summary

  • $_config protected
    array
    Contains the configuration params for this connection.
  • $_driver protected
    Cake\Database\Driver

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

  • $_logQueries protected
    boolean
    Whether to log queries generated during this connection.
  • $_logger protected
    Cake\Database\Log\QueryLogger|null
    Logger object instance.
  • $_schemaCollection protected
    Cake\Database\Schema\Collection|null
    The schema collection object
  • $_transactionLevel protected
    integer
    Contains how many nested transactions have been started.
  • $_transactionStarted protected
    boolean
    Whether a transaction is active in this connection.
  • $_useSavePoints protected
    boolean

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

  • $nestedTransactionRollbackException protected
    Cake\Database\Exception\NestedTransactionRollbackException|null

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

Method Summary

  • __construct() public
    Constructor.
  • __debugInfo() public

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

  • __destruct() public
    Destructor
  • _newLogger() protected

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

  • begin() public
    Starts a new transaction.
  • cacheMetadata() public
    Enables or disables metadata caching for this connection
  • commit() public
    Commits current transaction.
  • compileQuery() public

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

  • config() public
    Get the configuration data used to create the connection.
  • configName() public
    Get the configuration name for this connection.
  • connect() public
    Connects to the configured database.
  • createSavePoint() public
    Creates a new save point for nested transactions.
  • delete() public
    Executes a DELETE statement on the specified table.
  • disableConstraints() public
    Run an operation with constraints disabled.
  • disableForeignKeys() public
    Run driver specific SQL to disable foreign key checks.
  • disableQueryLogging() public
    Disable query logging
  • disableSavePoints() public
    Disables the usage of savepoints.
  • disconnect() public
    Disconnects from database server.
  • driver() public deprecated

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

  • enableForeignKeys() public
    Run driver specific SQL to enable foreign key checks.
  • enableQueryLogging() public
    Enable/disable query logging
  • enableSavePoints() public
    Enables/disables the usage of savepoints, enables only if driver the allows it.
  • execute() public

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

  • getDisconnectRetry() public

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

  • getDriver() public
    Gets the driver instance.
  • getLogger() public
    Gets the logger object
  • getSchemaCollection() public
    Gets a Schema\Collection object for this connection.
  • inTransaction() public
    Checks if a transaction is running.
  • insert() public
    Executes an INSERT query on the specified table.
  • isConnected() public
    Returns whether connection to database server was already established.
  • isQueryLoggingEnabled() public
    Check if query logging is enabled.
  • isSavePointsEnabled() public
    Returns whether this connection is using savepoints for nested transactions
  • log() public
    Logs a Query string using the configured logger object.
  • logQueries() public deprecated
    Enables or disables query logging for this connection.
  • logger() public deprecated

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

  • newQuery() public
    Create a new Query instance for this connection.
  • prepare() public
    Prepares a SQL statement to be executed.
  • query() public
    Executes a SQL statement and returns the Statement object as result.
  • quote() public
    Quotes value to be used safely in database query.
  • quoteIdentifier() public

    Quotes a database identifier (a column name, table name, etc..) to be used safely in queries without the risk of using reserved words.

  • releaseSavePoint() public
    Releases a save point by its name.
  • rollback() public
    Rollback current transaction.
  • rollbackSavepoint() public
    Rollback a save point by its name.
  • run() public

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

  • schemaCollection() public deprecated
    Gets or sets a Schema\Collection object for this connection.
  • setDriver() public

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

  • setLogger() public
    Sets a logger
  • setSchemaCollection() public
    Sets a Schema\Collection object for this connection.
  • supportsDynamicConstraints() public

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

  • supportsQuoting() public
    Checks if the driver supports quoting.
  • transactional() public

    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.

  • update() public
    Executes an UPDATE statement on the specified table.
  • useSavePoints() public deprecated

    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.

  • wasNestedTransactionRolledback() protected
    Returns whether some nested transaction has been already rolled back.

Method Detail

__construct() public ¶

__construct( array $config )

Constructor.

Parameters
array $config
configuration for connecting to database

__debugInfo() public ¶

__debugInfo( )

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

Returns
array

__destruct() public ¶

__destruct( )

Destructor

Disconnects the driver to release the connection.

_newLogger() 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

begin() public ¶

begin( )

Starts a new transaction.

cacheMetadata() 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 metadata caching, or true to use _cake_model_ or the name of the cache config to use.

commit() public ¶

commit( )

Commits current transaction.

Returns
boolean
true on success, false otherwise

compileQuery() 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

config() public ¶

config( )

Get the configuration data used to create the connection.

Returns
array
Implementation of
Cake\Datasource\ConnectionInterface::config()

configName() public ¶

configName( )

Get the configuration name for this connection.

Returns
string
Implementation of
Cake\Datasource\ConnectionInterface::configName()

connect() public ¶

connect( )

Connects to the configured database.

Returns
boolean
true, if the connection was already established or the attempt was successful.
Throws
Cake\Database\Exception\MissingConnectionException
if credentials are invalid.

createSavePoint() public ¶

createSavePoint( string $name )

Creates a new save point for nested transactions.

Parameters
string $name
The save point name.

delete() 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

disableConstraints() 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()

disableForeignKeys() public ¶

disableForeignKeys( )

Run driver specific SQL to disable foreign key checks.

disableQueryLogging() public ¶

disableQueryLogging( )

Disable query logging

Returns

$this

disableSavePoints() public ¶

disableSavePoints( )

Disables the usage of savepoints.

Returns

$this

disconnect() public ¶

disconnect( )

Disconnects from database server.

driver() public deprecated ¶

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.

Deprecated
3.4.0 Use setDriver()/getDriver() instead.
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
Throws
Cake\Database\Exception\MissingDriverException
When a driver class is missing.
Cake\Database\Exception\MissingExtensionException
When a driver's PHP extension is missing.

enableForeignKeys() public ¶

enableForeignKeys( )

Run driver specific SQL to enable foreign key checks.

enableQueryLogging() public ¶

enableQueryLogging( boolean $value )

Enable/disable query logging

Parameters
boolean $value
Enable/disable query logging
Returns

$this

enableSavePoints() public ¶

enableSavePoints( boolean $enable )

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

execute() 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

getDisconnectRetry() public ¶

getDisconnectRetry( )

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() public ¶

getDriver( )

Gets the driver instance.

Returns
Cake\Database\Driver

getLogger() public ¶

getLogger( )

Gets the logger object

Returns
Cake\Database\Log\QueryLogger
logger instance

getSchemaCollection() public ¶

getSchemaCollection( )

Gets a Schema\Collection object for this connection.

Returns
Cake\Database\Schema\Collection

inTransaction() public ¶

inTransaction( )

Checks if a transaction is running.

Returns
boolean
True if a transaction is running else false.

insert() 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

isConnected() public ¶

isConnected( )

Returns whether connection to database server was already established.

Returns
boolean

isQueryLoggingEnabled() public ¶

isQueryLoggingEnabled( )

Check if query logging is enabled.

Returns
boolean

isSavePointsEnabled() public ¶

isSavePointsEnabled( )

Returns whether this connection is using savepoints for nested transactions

Returns
boolean
true if enabled, false otherwise

log() public ¶

log( string $sql )

Logs a Query string using the configured logger object.

Parameters
string $sql
string to be logged

logQueries() public deprecated ¶

logQueries( boolean|null $enable = null )

Enables or disables query logging for this connection.

Deprecated
3.7.0 Use enableQueryLogging() and isQueryLoggingEnabled() instead.
Parameters
boolean|null $enable optional null

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

Returns
boolean
Implementation of
Cake\Datasource\ConnectionInterface::logQueries()

logger() public deprecated ¶

logger( object|null $instance = null )

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

Deprecated
3.5.0 Use getLogger() and setLogger() instead.
Parameters
object|null $instance optional null
logger object instance
Returns
object
logger instance
Implementation of
Cake\Datasource\ConnectionInterface::logger()

newQuery() public ¶

newQuery( )

Create a new Query instance for this connection.

Returns
Cake\Database\Query

prepare() 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

query() 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

quote() public ¶

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

Quotes value to be used safely in database query.

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

quoteIdentifier() 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

releaseSavePoint() public ¶

releaseSavePoint( string $name )

Releases a save point by its name.

Parameters
string $name
The save point name.

rollback() public ¶

rollback( boolean|null $toBeginning = null )

Rollback current transaction.

Parameters
boolean|null $toBeginning optional null

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

rollbackSavepoint() public ¶

rollbackSavepoint( string $name )

Rollback a save point by its name.

Parameters
string $name
The save point name.

run() 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

schemaCollection() public deprecated ¶

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

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

Deprecated
3.4.0 Use setSchemaCollection()/getSchemaCollection()
Parameters
Cake\Database\Schema\Collection $collection optional null
The schema collection object
Returns
Cake\Database\Schema\Collection

setDriver() public ¶

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

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 optional []
Config for a new driver.
Returns

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

setLogger() public ¶

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

Sets a logger

Parameters
Cake\Database\Log\QueryLogger $logger
Logger object
Returns

$this

setSchemaCollection() public ¶

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

Sets a Schema\Collection object for this connection.

Parameters
Cake\Database\Schema\Collection $collection
The schema collection object
Returns

$this

supportsDynamicConstraints() public ¶

supportsDynamicConstraints( )

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

Returns
boolean
true if driver supports dynamic constraints

supportsQuoting() public ¶

supportsQuoting( )

Checks if the driver supports quoting.

Returns
boolean

transactional() 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()

update() 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

useSavePoints() public deprecated ¶

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

Deprecated
3.4.0 Use enableSavePoints()/isSavePointsEnabled() instead.
Parameters
boolean|null $enable optional null
Whether or not save points should be used.
Returns
boolean
true if enabled, false otherwise

wasNestedTransactionRolledback() protected ¶

wasNestedTransactionRolledback( )

Returns whether some nested transaction has been already rolled back.

Returns
boolean

Methods used from Cake\Database\TypeConverterTrait

cast() 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

matchTypes() 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

Magic methods inherited from Cake\Datasource\ConnectionInterface

execute(), getLogger(), getSchemaCollection(), isQueryLoggingEnabled(), newQuery(), prepare(), quote(), supportsDynamicConstraints()

Properties detail

$_config ¶

protected array

Contains the configuration params for this connection.

$_driver ¶

protected Cake\Database\Driver

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

$_logQueries ¶

protected boolean

Whether to log queries generated during this connection.

false

$_logger ¶

protected Cake\Database\Log\QueryLogger|null

Logger object instance.

$_schemaCollection ¶

protected Cake\Database\Schema\Collection|null

The schema collection object

$_transactionLevel ¶

protected integer

Contains how many nested transactions have been started.

$_transactionStarted ¶

protected boolean

Whether a transaction is active in this connection.

false

$_useSavePoints ¶

protected boolean

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

false

$nestedTransactionRollbackException ¶

protected Cake\Database\Exception\NestedTransactionRollbackException|null

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

Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs