DbConnection

Type
Class
Namespace
Craft
Inherits
Craft\DbConnection » CDbConnection (opens new window) » CApplicationComponent (opens new window) » CComponent (opens new window)
Implements
IApplicationComponent
Since
1.0

Class DbConnection

See also http://craftcms.com

View source (opens new window)

# Public Properties

Property Description
$autoConnect (opens new window) boolean (opens new window) – Whether the database connection should be automatically established the component is being initialized.
$behaviors (opens new window) array (opens new window) – The behaviors that should be attached to this component.
$charset (opens new window) string (opens new window) – The charset used for database connection.
$connectionString (opens new window) string (opens new window) – The Data Source Name, or DSN, contains the information required to connect to the database.
$driverMap (opens new window) array (opens new window) – Mapping between PDO driver and schema class name.
$emulatePrepare (opens new window) boolean (opens new window) – Whether to turn on prepare emulation.
$enableParamLogging (opens new window) boolean (opens new window) – Whether to log the values that are bound to a prepare SQL statement.
$enableProfiling (opens new window) boolean (opens new window) – Whether to enable profiling the SQL statements being executed.
$initSQLs (opens new window) array (opens new window) – List of SQL statements that should be executed right after the DB connection is established.
$password (opens new window) string (opens new window) – The password for establishing DB connection.
$pdoClass (opens new window) string (opens new window) – Custom PDO wrapper class.
$queryCacheID (opens new window) string (opens new window) – The ID of the cache application component that is used for query caching.
$queryCachingCount (opens new window) integer (opens new window) – The number of SQL statements that need to be cached next.
$queryCachingDependency (opens new window) CCacheDependency (opens new window), \ICacheDependency – The dependency that will be used when saving query results into cache.
$queryCachingDuration (opens new window) integer (opens new window) – Number of seconds that query results can remain valid in cache.
$schemaCacheID (opens new window) string (opens new window) – The ID of the cache application component that is used to cache the table metadata.
$schemaCachingDuration (opens new window) integer (opens new window) – Number of seconds that table metadata can remain valid in cache.
$schemaCachingExclude (opens new window) array (opens new window) – List of tables whose metadata should NOT be cached.
$tablePrefix (opens new window) string (opens new window) – The default prefix for table names.
$username (opens new window) string (opens new window) – The username for establishing DB connection.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns a property value, an event handler list or a behavior based on its name.
__isset() (opens new window) Checks if a property value is null.
__set() (opens new window) Sets value of a component property.
__sleep() (opens new window) Close the connection when serializing.
__unset() (opens new window) Sets a component property to be null.
addTablePrefix() Adds the table prefix to the passed-in table name(s).
asa() (opens new window) Returns the named behavior object.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
attachEventHandler() (opens new window) Attaches an event handler to an event.
backup() Performs a database backup.
beginTransaction() (opens new window) Starts a transaction.
cache() (opens new window) Sets the parameters about query caching.
canGetProperty() (opens new window) Determines whether a property can be read.
canSetProperty() (opens new window) Determines whether a property can be set.
columnExists() Checks if a column exists in a table.
createCommand()
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
detachEventHandler() (opens new window) Detaches an existing event handler.
disableBehavior() (opens new window) Disables an attached behavior.
disableBehaviors() (opens new window) Disables all behaviors attached to this component.
enableBehavior() (opens new window) Enables an attached behavior.
enableBehaviors() (opens new window) Enables all behaviors attached to this component.
evaluateExpression() (opens new window) Evaluates a PHP expression or callback under the context of this component.
getActive() (opens new window) Returns whether the DB connection is established.
getAttribute() (opens new window) Obtains a specific DB connection attribute information.
getAttributes() (opens new window) Returns the attributes that are previously explicitly set for the DB connection.
getAutoCommit() (opens new window) Returns whether creating or updating a DB record will be automatically committed.
getAvailableDrivers() (opens new window) Returns a list of available PDO drivers.
getClientVersion() (opens new window) Returns the version information of the DB driver.
getColumnCase() (opens new window) Returns the case of the column names
getCommandBuilder() (opens new window) Returns the SQL command builder for the current DB connection.
getConnectionStatus() (opens new window) Returns the status of the connection.
getCurrentTransaction() (opens new window) Returns the currently active transaction.
getDriverName() (opens new window) Returns the name of the DB driver.
getEventHandlers() (opens new window) Returns the list of attached event handlers for an event.
getForeignKeyName() Returns a foreign key name based on the table and column names.
getIndexName() Returns an index name based on the table, column names, and whether it should be unique.
getIsInitialized() (opens new window) Checks if this application component has been initialized.
getLastInsertID() (opens new window) Returns the ID of the last inserted row or sequence value.
getNormalizedTablePrefix()
getNullConversion() (opens new window) Returns how the null and empty strings are converted.
getPdoInstance() (opens new window) Returns the PDO instance.
getPdoType() (opens new window) Determines the PDO type for the specified PHP type.
getPersistent() (opens new window) Returns whether the connection is persistent or not.
getPrefetch() (opens new window) Returns whether the connection performs data prefetching.
getPrimaryKeyName() Returns a primary key name based on the table and column names.
getSchema() (opens new window) Returns the database schema for the current connection
getServerInfo() (opens new window) Returns the information of DBMS server.
getServerVersion() (opens new window) Returns the version information of DBMS server.
getStats() (opens new window) Returns the statistical results of SQL executions.
getTimeout() (opens new window) Returns the timeout settings for the connection.
hasEvent() (opens new window) Determines whether an event is defined.
hasEventHandler() (opens new window) Checks whether the named event has attached handlers.
hasProperty() (opens new window) Determines whether a property is defined.
init() (opens new window) Initializes the application component.
onBackup() Fires an 'onBackup' event.
quoteColumnName() (opens new window) Quotes a column name for use in a query.
quoteDatabaseName()
quoteTableName() (opens new window) Quotes a table name for use in a query.
quoteValue() (opens new window) Quotes a string value for use in a query.
quoteValueWithType() (opens new window) Quotes a value for use in a query using a given type.
raiseEvent() (opens new window) Raises an event.
setActive() (opens new window) Open or close the DB connection.
setAttribute() (opens new window) Sets an attribute on the database connection.
setAttributes() (opens new window) Sets a set of attributes on the database connection.
setAutoCommit() (opens new window) Sets whether creating or updating a DB record will be automatically committed.
setColumnCase() (opens new window) Sets the case of the column names.
setDriverName() (opens new window) Changes the name of the DB driver. Overrides value extracted from the {@link connectionString}, which is behavior by default.
setNullConversion() (opens new window) Sets how the null and empty strings are converted.
setPersistent() (opens new window) Sets whether the connection is persistent or not.
tableExists() Returns whether a table exists.
trimObjectName() Ensures that an object name is within the schema's limit.

# addTablePrefix()

Adds the table prefix to the passed-in table name(s).

View source (opens new window)

Arguments

Returns

string (opens new window), array (opens new window) – The modified table name(s)

Signature

public string, array addTablePrefix ( $table )

# backup()

Performs a database backup.

View source (opens new window)

Arguments

  • $ignoreDataTables (array (opens new window), null (opens new window)) – If set to an empty array, a full database backup will be performed. If set to an array or database table names, they will get merged with the default list of table names whose data is to be ignored during a database backup.

Returns

boolean (opens new window), string (opens new window) – The file path to the database backup, or false if something went wrong.

Signature

public boolean, string backup ( $ignoreDataTables = null )

# columnExists()

Signature

public boolean columnExists ( $table, $column, $refresh = null )

# createCommand()

Signature

public Craft\DbCommand createCommand ( $query = null )

# getForeignKeyName()

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

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public string getForeignKeyName ( $table, $columns )

# getIndexName()

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

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public string getIndexName ( $table, $columns, $unique = false )

# getNormalizedTablePrefix()

Signature

public string getNormalizedTablePrefix ( )

# getPrimaryKeyName()

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

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public string getPrimaryKeyName ( $table, $columns )

# onBackup()

Fires an 'onBackup' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onBackup ( Craft\Event $event )

# quoteDatabaseName()

Signature

public string quoteDatabaseName ( $name )

# tableExists()

Signature

public boolean tableExists ( $table, $refresh = null )

# trimObjectName()

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

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public string trimObjectName ( $name )

# Protected Methods

Method Description
close() (opens new window) Closes the currently active DB connection.
createPdoInstance() (opens new window) Creates the PDO instance.
initConnection() (opens new window) Initializes the open db connection.
open() (opens new window) Opens DB connection if it is currently not