DbConnection

Type
Class
Namespace
Craft
Inherits
Craft\DbConnection » CDbConnection » CApplicationComponent » CComponent
Implements
IApplicationComponent
Since
1.0

Class DbConnection

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$autoConnectboolean – Whether the database connection should be automatically established the component is being initialized.
$behaviorsarray – The behaviors that should be attached to this component.
$charsetstring – The charset used for database connection.
$connectionStringstring – The Data Source Name, or DSN, contains the information required to connect to the database.
$driverMaparray – Mapping between PDO driver and schema class name.
$emulatePrepareboolean – Whether to turn on prepare emulation.
$enableParamLoggingboolean – Whether to log the values that are bound to a prepare SQL statement.
$enableProfilingboolean – Whether to enable profiling the SQL statements being executed.
$initSQLsarray – List of SQL statements that should be executed right after the DB connection is established.
$passwordstring – The password for establishing DB connection.
$pdoClassstring – Custom PDO wrapper class.
$queryCacheIDstring – The ID of the cache application component that is used for query caching.
$queryCachingCountinteger – The number of SQL statements that need to be cached next.
$queryCachingDependencyCCacheDependency, \ICacheDependency – The dependency that will be used when saving query results into cache.
$queryCachingDurationinteger – Number of seconds that query results can remain valid in cache.
$schemaCacheIDstring – The ID of the cache application component that is used to cache the table metadata.
$schemaCachingDurationinteger – Number of seconds that table metadata can remain valid in cache.
$schemaCachingExcludearray – List of tables whose metadata should NOT be cached.
$tablePrefixstring – The default prefix for table names.
$usernamestring – The username for establishing DB connection.

Public Methods

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

Arguments

  • $table (string, array) – The table name or an array of table names

Returns

string, array – The modified table name(s)

Signature

public string, array addTablePrefix ( $table )

backup()

Performs a database backup.

View source

Arguments

  • $ignoreDataTables (array, null) – 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, string – The file path to the database backup, or false if something went wrong.

Signature

public boolean, string backup ( $ignoreDataTables = null )

columnExists()

Checks if a column exists in a table.

View source

Arguments

Returns

boolean

Signature

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

createCommand()

View source

Arguments

Returns

Craft\DbCommand

Signature

public Craft\DbCommand createCommand ( $query = null )

getForeignKeyName()

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

View source

Arguments

Returns

string

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

Arguments

Returns

string

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

Arguments

Returns

string

Signature

public string getPrimaryKeyName ( $table, $columns )

onBackup()

Fires an 'onBackup' event.

View source

Arguments

Returns

null

Signature

public null onBackup ( Craft\Event $event )

quoteDatabaseName()

View source

Arguments

  • $name

Returns

string

Signature

public string quoteDatabaseName ( $name )

tableExists()

Returns whether a table exists.

View source

Arguments

Returns

boolean

Signature

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

trimObjectName()

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

View source

Arguments

Returns

string

Signature

public string trimObjectName ( $name )

Protected Methods

MethodDescription
close()Closes the currently active DB connection.
createPdoInstance()Creates the PDO instance.
initConnection()Initializes the open db connection.
open()Opens DB connection if it is currently not