Craft 3 Class Reference

Class craft\db\Connection

Inheritance
craft\db\Connection » yii\db\Connection » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/db/Connection.php

Public Properties
Property Type Description Defined By
$attributes array PDO attributes (name => value) that should be set when calling open() to establish a DB connection. yii\db\Connection
$charset string The charset used for database connection. yii\db\Connection
$commandClass string The class used to create new database yii\db\Command objects. yii\db\Connection
$commandMap array Mapping between PDO driver names and yii\db\Command classes. yii\db\Connection
$dsn string The Data Source Name, or DSN, contains the information required to connect to the database. yii\db\Connection
$emulatePrepare boolean Whether to turn on prepare emulation. yii\db\Connection
$enableLogging boolean Whether to enable logging of database queries. yii\db\Connection
$enableProfiling boolean Whether to enable profiling of opening database connection and database queries. yii\db\Connection
$enableQueryCache boolean Whether to enable query caching. yii\db\Connection
$enableSavepoint boolean Whether to enable [savepoint](http://en. yii\db\Connection
$enableSchemaCache boolean Whether to enable schema caching. yii\db\Connection
$enableSlaves boolean Whether to enable read/write splitting by using $slaves to read data. yii\db\Connection
$masterConfig array The configuration that should be merged with every master configuration listed in $masters. yii\db\Connection
$masters array List of master connection configurations. yii\db\Connection
$password string The password for establishing DB connection. yii\db\Connection
$pdo PDO The PHP PDO instance associated with this DB connection. yii\db\Connection
$pdoClass string Custom PDO wrapper class. yii\db\Connection
$queryCache yii\caching\CacheInterface, string The cache object or the ID of the cache application component that is used for query caching. yii\db\Connection
$queryCacheDuration integer The default number of seconds that query results can remain valid in cache. yii\db\Connection
$schemaCache yii\caching\CacheInterface, string The cache object or the ID of the cache application component that is used to cache the table metadata. yii\db\Connection
$schemaCacheDuration integer Number of seconds that table metadata can remain valid in cache. yii\db\Connection
$schemaCacheExclude array List of tables whose metadata should NOT be cached. yii\db\Connection
$schemaMap array Mapping between PDO driver names and yii\db\Schema classes. yii\db\Connection
$serverRetryInterval integer The retry interval in seconds for dead servers listed in $masters and $slaves. yii\db\Connection
$serverStatusCache yii\caching\CacheInterface, string, false The cache object or the ID of the cache application component that is used to store the health status of the DB servers specified in $masters and $slaves. yii\db\Connection
$shuffleMasters boolean Whether to shuffle $masters before getting one. yii\db\Connection
$slaveConfig array The configuration that should be merged with every slave configuration listed in $slaves. yii\db\Connection
$slaves array List of slave connection configurations. yii\db\Connection
$tablePrefix string The common prefix or suffix for table names. yii\db\Connection
$username string The username for establishing DB connection. yii\db\Connection
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__sleep() Close the connection before serializing. yii\db\Connection
__unset() Sets an object property to null. yii\base\BaseObject
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
backup() Performs a backup operation. If a backupCommand config setting has been set, will execute it. If not, will execute the default database schema specific backup defined in getDefaultBackupCommand(), which uses pg_dump for PostgreSQL and mysqldump for MySQL. craft\db\Connection
backupTo() Performs a backup operation. If a backupCommand config setting has been set, will execute it. If not, will execute the default database schema specific backup defined in getDefaultBackupCommand(), which uses pg_dump for PostgreSQL and mysqldump for MySQL. craft\db\Connection
beginTransaction() Starts a transaction. yii\db\Connection
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
cache() Uses query cache for the queries performed with the callable. yii\db\Connection
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
close() Closes the currently active DB connection. yii\db\Connection
columnExists() Checks if a column exists in a table. craft\db\Connection
createCommand() Creates a command for execution. yii\db\Connection
createFromConfig() Creates a new Connection instance based off the given DbConfig object. craft\db\Connection
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getDriverName() Returns the name of the DB driver. Based on the the current $dsn, in case it was not set explicitly by an end user. yii\db\Connection
getForeignKeyName() Returns a foreign key name based on the table and column names. craft\db\Connection
getIndexName() Returns an index name based on the table, column names, and whether it should be unique. craft\db\Connection
getIsActive() Returns a value indicating whether the DB connection is established. yii\db\Connection
getIsMysql() Returns whether this is a MySQL connection. craft\db\Connection
getIsPgsql() Returns whether this is a PostgreSQL connection. craft\db\Connection
getLastInsertID() Returns the ID of the last inserted row or sequence value. yii\db\Connection
getMaster() Returns the currently active master connection. yii\db\Connection
getMasterPdo() Returns the PDO instance for the currently active master connection. yii\db\Connection
getPrimaryKeyName() Returns a primary key name based on the table and column names. craft\db\Connection
getQueryBuilder() Returns the query builder for the current DB connection. yii\db\Connection
getQueryCacheInfo() Returns the current query cache information. yii\db\Connection
getSchema() Returns the schema information for the database opened by this connection. yii\db\Connection
getServerVersion() Returns a server version as a string comparable by \version_compare(). yii\db\Connection
getSlave() Returns the currently active slave connection. yii\db\Connection
getSlavePdo() Returns the PDO instance for the currently active slave connection. yii\db\Connection
getSupportsMb4() Returns whether the database supports 4+ byte characters. craft\db\Connection
getTableSchema() Obtains the schema information for the named table. yii\db\Connection
getTransaction() Returns the currently active transaction. yii\db\Connection
getVersion() Returns the version of the DB. craft\db\Connection
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
noCache() Disables query cache temporarily. yii\db\Connection
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
open() Establishes a DB connection. craft\db\Connection
quoteColumnName() Quotes a column name for use in a query. yii\db\Connection
quoteDatabaseName() craft\db\Connection
quoteSql() Processes a SQL statement by quoting table and column names that are enclosed within double brackets. yii\db\Connection
quoteTableName() Quotes a table name for use in a query. yii\db\Connection
quoteValue() Quotes a string value for use in a query. yii\db\Connection
restore() Restores a database at the given file path. craft\db\Connection
setDriverName() Changes the current driver name. yii\db\Connection
setQueryBuilder() Can be used to set yii\db\QueryBuilder configuration via Connection configuration array. yii\db\Connection
setSupportsMb4() Sets whether the database supports 4+ byte characters. craft\db\Connection
tableExists() Returns whether a table exists. craft\db\Connection
transaction() Executes callback provided in a transaction. yii\db\Connection
trigger() Triggers an event. yii\base\Component
trimObjectName() Ensures that an object name is within the schema's limit. craft\db\Connection
useMaster() Executes the provided callback by using the master connection. yii\db\Connection
Protected Methods
Method Description Defined By
createPdoInstance() Creates the PDO instance. yii\db\Connection
initConnection() Initializes the DB connection. yii\db\Connection
openFromPool() Opens the connection to a server in the pool. yii\db\Connection
openFromPoolSequentially() Opens the connection to a server in the pool. yii\db\Connection
Events
Event Type Description Defined By
EVENT_AFTER_CREATE_BACKUP craft\events\BackupEvent The event that is triggered after the backup is created. craft\db\Connection
EVENT_AFTER_OPEN \yii\db\[[yii\base\Event, \yii\db\Event]] An event that is triggered after a DB connection is established yii\db\Connection
EVENT_AFTER_RESTORE_BACKUP craft\events\RestoreEvent The event that is triggered after the restore occurred. craft\db\Connection
EVENT_BEFORE_CREATE_BACKUP craft\events\BackupEvent The event that is triggered before the backup is created. craft\db\Connection
EVENT_BEFORE_RESTORE_BACKUP craft\events\RestoreEvent The event that is triggered before the restore is started. craft\db\Connection
EVENT_BEGIN_TRANSACTION \yii\db\[[yii\base\Event, \yii\db\Event]] An event that is triggered right before a top-level transaction is started yii\db\Connection
EVENT_COMMIT_TRANSACTION \yii\db\[[yii\base\Event, \yii\db\Event]] An event that is triggered right after a top-level transaction is committed yii\db\Connection
EVENT_ROLLBACK_TRANSACTION \yii\db\[[yii\base\Event, \yii\db\Event]] An event that is triggered right after a top-level transaction is rolled back yii\db\Connection

Method Details

backup() public method #

Performs a backup operation. If a backupCommand config setting has been set, will execute it. If not, will execute the default database schema specific backup defined in getDefaultBackupCommand(), which uses pg_dump for PostgreSQL and mysqldump for MySQL.

public string backup ( )
return string The file path to the database backup
throws yii\base\Exception if the backupCommand config setting is false
throws craft\errors\ShellCommandException in case of failure

backupTo() public method #

Performs a backup operation. If a backupCommand config setting has been set, will execute it. If not, will execute the default database schema specific backup defined in getDefaultBackupCommand(), which uses pg_dump for PostgreSQL and mysqldump for MySQL.

public void backupTo ( \craft\db\string $filePath )
$filePath string The file path the database backup should be saved at
throws yii\base\Exception if the backupCommand config setting is false
throws craft\errors\ShellCommandException in case of failure

columnExists() public method #

Checks if a column exists in a table.

public boolean columnExists ( \craft\db\string $table, \craft\db\string $column, \craft\db\bool $refresh null )
$table string
$column string
$refresh boolean, null
throws yii\base\NotSupportedException if there is no support for the current driver type

createFromConfig() public static method #

Creates a new Connection instance based off the given DbConfig object.

public static static createFromConfig ( craft\config\DbConfig $config )
$config craft\config\DbConfig

getForeignKeyName() public method #

Returns a foreign key name based on the table and column names.

public string getForeignKeyName ( \craft\db\string $table, $columns )
$table string
$columns string, array

getIndexName() public method #

Returns an index name based on the table, column names, and whether it should be unique.

public string getIndexName ( \craft\db\string $table, $columns, \craft\db\bool $unique false, \craft\db\bool $foreignKey false )
$table string
$columns string, array
$unique boolean
$foreignKey boolean

getIsMysql() public method #

Returns whether this is a MySQL connection.

public boolean getIsMysql ( )

getIsPgsql() public method #

Returns whether this is a PostgreSQL connection.

public boolean getIsPgsql ( )

getPrimaryKeyName() public method #

Returns a primary key name based on the table and column names.

public string getPrimaryKeyName ( \craft\db\string $table, $columns )
$table string
$columns string, array

getSupportsMb4() public method #

Returns whether the database supports 4+ byte characters.

public boolean getSupportsMb4 ( )

getVersion() public method #

Returns the version of the DB.

public string getVersion ( )

open() public method #

Establishes a DB connection.

It does nothing if a DB connection has already been established.

public void open ( )
throws craft\errors\DbConnectException if there are any issues
throws Throwable

quoteDatabaseName() public method #

public string quoteDatabaseName ( \craft\db\string $name )
$name string

restore() public method #

Restores a database at the given file path.

public void restore ( \craft\db\string $filePath )
$filePath string The path of the database backup to restore.
throws yii\base\Exception if the restoreCommand config setting is false
throws craft\errors\ShellCommandException in case of failure

setSupportsMb4() public method #

Sets whether the database supports 4+ byte characters.

public void setSupportsMb4 ( \craft\db\bool $supportsMb4 )
$supportsMb4 boolean

tableExists() public method #

Returns whether a table exists.

public boolean tableExists ( \craft\db\string $table, \craft\db\bool $refresh null )
$table string
$refresh boolean, null

trimObjectName() public method #

Ensures that an object name is within the schema's limit.

public string trimObjectName ( \craft\db\string $name )
$name string

Event Details

EVENT_AFTER_CREATE_BACKUP event of type craft\events\BackupEvent #

The event that is triggered after the backup is created.

EVENT_AFTER_RESTORE_BACKUP event of type craft\events\RestoreEvent #

The event that is triggered after the restore occurred.

EVENT_BEFORE_CREATE_BACKUP event of type craft\events\BackupEvent #

The event that is triggered before the backup is created.

EVENT_BEFORE_RESTORE_BACKUP event of type craft\events\RestoreEvent #

The event that is triggered before the restore is started.