Connection

Type
Class
Namespace
craft\db
Inherits
craft\db\Connection » yii\db\Connection » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

View source

Public Properties

PropertyDescription
attributesarray – PDO attributes (name => value) that should be set when calling open() to establish a DB connection.
backupFilePathstring
behaviorsyii\base\Behavior – List of behaviors attached to this component
charsetstring – The charset used for database connection.
commandClassstring – The class used to create new database craft\db\Command objects.
commandMaparray – Mapping between PDO driver names and craft\db\Command classes.
driverNamestring – Name of the DB driver
dsnstring – The Data Source Name, or DSN, contains the information required to connect to the database.
emulatePrepareboolean – Whether to turn on prepare emulation.
enableLoggingboolean – Whether to enable logging of database queries.
enableProfilingboolean – Whether to enable profiling of opening database connection and database queries.
enableQueryCacheboolean – Whether to enable query caching.
enableSavepointboolean – Whether to enable [savepoint](http://en.
enableSchemaCacheboolean – Whether to enable schema caching.
enableSlavesboolean – Whether to enable read/write splitting by using slaves to read data.
ignoredBackupTablesstring[]
isActiveboolean – Whether the DB connection is established
isMysqlboolean
isPgsqlboolean
lastInsertIDstring – The row ID of the last row inserted, or the last value retrieved from the sequence object
masteryii\db\Connection – The currently active master connection.
masterConfigarray – The configuration that should be merged with every master configuration listed in masters.
masterPdoPDO – The PDO instance for the currently active master connection.
mastersarray – List of master connection configurations.
passwordstring – The password for establishing DB connection.
pdoPDO – The PHP PDO instance associated with this DB connection.
pdoClassstring – Custom PDO wrapper class.
queryBuilderyii\db\QueryBuilder – The query builder for the current DB connection.
queryCacheyii\caching\CacheInterface, string – The cache object or the ID of the cache application component that is used for query caching.
queryCacheDurationinteger – The default number of seconds that query results can remain valid in cache.
schemayii\db\Schema – The schema information for the database opened by this connection.
schemaCacheyii\caching\CacheInterface, string – The cache object or the ID of the cache application component that is used to cache the table metadata.
schemaCacheDurationinteger – Number of seconds that table metadata can remain valid in cache.
schemaCacheExcludearray – List of tables whose metadata should NOT be cached.
schemaMaparray – Mapping between PDO driver names and \craft\db\Schema classes.
serverRetryIntervalinteger – The retry interval in seconds for dead servers listed in masters and slaves.
serverStatusCacheyii\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.
serverVersionstring – Server version as a string.
shuffleMastersboolean – Whether to shuffle masters before getting one.
slaveyii\db\Connection – The currently active slave connection.
slaveConfigarray – The configuration that should be merged with every slave configuration listed in slaves.
slavePdoPDO – The PDO instance for the currently active slave connection.
slavesarray – List of slave connection configurations.
supportsMb4boolean
tablePrefixstring – The common prefix or suffix for table names.
transactionyii\db\Transaction, null – The currently active transaction.
usernamestring – The username for establishing DB connection.
versionstring

backupFilePath

Type
string
Access
Read-only

View source

ignoredBackupTables

Type
string[]
Access
Read-only

View source

isMysql

Type
boolean
Access
Read-only

View source

isPgsql

Type
boolean
Access
Read-only

View source

supportsMb4

Type
boolean

View source

version

Type
string
Access
Read-only

View source

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__sleep()Close the connection before serializing.
__unset()Sets an object property to null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the 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.
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.
beginTransaction()Starts a transaction.
behaviors()Returns a list of behaviors that this component should behave as.
cache()Uses query cache for the queries performed with the callable.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
close()Closes the currently active DB connection.
columnExists()Checks if a column exists in a table.
createCommand()Creates a command for execution.
createFromConfig()Creates a new Connection instance based off the given DbConfig object.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getBackupFilePath()Returns the path for a new backup file.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this 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.
getForeignKeyName()Returns a foreign key name based on the table and column names.
getIgnoredBackupTables()Returns the raw database table names that should be ignored by default.
getIndexName()Returns an index name based on the table, column names, and whether it should be unique.
getIsActive()Returns a value indicating whether the DB connection is established.
getIsMysql()Returns whether this is a MySQL connection.
getIsPgsql()Returns whether this is a PostgreSQL connection.
getLastInsertID()Returns the ID of the last inserted row or sequence value.
getMaster()Returns the currently active master connection.
getMasterPdo()Returns the PDO instance for the currently active master connection.
getPrimaryKeyName()Returns a primary key name based on the table and column names.
getQueryBuilder()Returns the query builder for the current DB connection.
getSchema()Returns the schema information for the database opened by this connection.
getServerVersion()Returns a server version as a string comparable by \version_compare().
getSlave()Returns the currently active slave connection.
getSlavePdo()Returns the PDO instance for the currently active slave connection.
getSupportsMb4()Returns whether the database supports 4+ byte characters.
getTableSchema()Obtains the schema information for the named table.
getTransaction()Returns the currently active transaction.
getVersion()Returns the version of the DB.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
noCache()Disables query cache temporarily.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
open()Establishes a DB connection.
quoteColumnName()Quotes a column name for use in a query.
quoteDatabaseName()
quoteSql()Processes a SQL statement by quoting table and column names that are enclosed within double brackets.
quoteTableName()Quotes a table name for use in a query.
quoteValue()Quotes a string value for use in a query.
restore()Restores a database at the given file path.
setDriverName()Changes the current driver name.
setQueryBuilder()Can be used to set \craft\db\QueryBuilder configuration via Connection configuration array.
setSupportsMb4()Sets whether the database supports 4+ byte characters.
tableExists()Returns whether a table exists.
transaction()Executes callback provided in a transaction.
trigger()Triggers an event.
trimObjectName()Ensures that an object name is within the schema's limit.
useMaster()Executes the provided callback by using the master connection.

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.

View source

Returns

string – The file path to the database backup

Throws

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.

View source

Arguments

  • $filePath (string) – The file path the database backup should be saved at

Throws

columnExists()

Checks if a column exists in a table.

View source

Arguments

Returns

boolean

Throws

createFromConfig()

DEPRECATED

Deprecated in 3.0.18. Use craft\helpers\App::dbConfig() instead.

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

View source

Arguments

Returns

static

getBackupFilePath()

Returns the path for a new backup file.

View source

Returns

string

getForeignKeyName()

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

View source

Arguments

Returns

string

getIgnoredBackupTables()

Returns the raw database table names that should be ignored by default.

View source

Returns

string[]

getIndexName()

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

View source

Arguments

Returns

string

getIsMysql()

Returns whether this is a MySQL connection.

View source

Returns

boolean

getIsPgsql()

Returns whether this is a PostgreSQL connection.

View source

Returns

boolean

getPrimaryKeyName()

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

View source

Arguments

Returns

string

getSupportsMb4()

Returns whether the database supports 4+ byte characters.

View source

Returns

boolean

getVersion()

Returns the version of the DB.

View source

Returns

string

open()

Establishes a DB connection.

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

View source

Throws

quoteColumnName()

Quotes a column name for use in a query.

If the column name contains prefix, the prefix will also be properly quoted. If the column name is already quoted or contains special characters including '(', '[[' and '{{', then this method will do nothing.

View source

Arguments

  • $name (string) – Column name

Returns

string – The properly quoted column name

quoteDatabaseName()

View source

Arguments

Returns

string

quoteTableName()

Quotes a table name for use in a query.

If the table name contains schema prefix, the prefix will also be properly quoted. If the table name is already quoted or contains special characters including '(', '[[' and '{{', then this method will do nothing.

View source

Arguments

Returns

string – The properly quoted table name

restore()

Restores a database at the given file path.

View source

Arguments

  • $filePath (string) – The path of the database backup to restore.

Throws

setSupportsMb4()

Sets whether the database supports 4+ byte characters.

View source

Arguments

tableExists()

Returns whether a table exists.

View source

Arguments

Returns

boolean

trimObjectName()

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

View source

Arguments

Returns

string

Protected Methods

MethodDescription
createPdoInstance()Creates the PDO instance.
initConnection()Initializes the DB connection.
openFromPool()Opens the connection to a server in the pool.
openFromPoolSequentially()Opens the connection to a server in the pool.

Events

EVENT_AFTER_CREATE_BACKUP

Type
craft\events\BackupEvent

The event that is triggered after the backup is created.

EVENT_AFTER_RESTORE_BACKUP

Type
craft\events\RestoreEvent

The event that is triggered after the restore occurred.

EVENT_BEFORE_CREATE_BACKUP

Type
craft\events\BackupEvent

The event that is triggered before the backup is created.

EVENT_BEFORE_RESTORE_BACKUP

Type
craft\events\RestoreEvent

The event that is triggered before the restore is started.