Property | Type | Description | |
---|---|---|---|
$attributes | PDO attributes (name => value) that should be set when calling Connection::open to establish a DB connection. Please refer to the PHP manual for details about available attributes. | ||
$charset | the charset used for database connection. The property is only used for MySQL, PostgreSQL and CUBRID databases. Defaults to null, meaning using default charset as configured by the database. For Oracle Database, the charset must be specified in the [[dsn]], for example for UTF-8 by appending ;charset=UTF-8 to the DSN string. The same applies for if you're using GBK or BIG5 charset with MySQL, then it's highly recommended to specify charset via [[dsn]] like 'mysql:dbname=mydatabase;host=127.0.0.1;charset=GBK;'. | ||
$commandClass | the class used to create new database Command objects. If you want to extend the Command class, you may configure this property to use your extended version of the class. | ||
$dsn | the Data Source Name, or DSN, contains the information required to connect to the database. Please refer to the PHP manual on the format of the DSN string. For SQLite you may use a path alias for specifying the database path, e.g. sqlite:@app/data/db.sql. | ||
$emulatePrepare | whether to turn on prepare emulation. Defaults to false, meaning PDO will use the native prepare support if available. For some databases (such as MySQL), this may need to be set true so that PDO can emulate the prepare support to bypass the buggy native prepare support. The default value is null, which means the PDO ATTR_EMULATE_PREPARES value will not be changed. | ||
$enableQueryCache | whether to enable query caching. Note that in order to enable query caching, a valid cache component as specified by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true. Also, only the results of the queries enclosed within Connection::cache will be cached. | ||
$enableSavepoint | whether to enable savepoint. Note that if the underlying DBMS does not support savepoint, setting this property to be true will have no effect. | ||
$enableSchemaCache | whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as specified by [[schemaCache]] must be enabled and [[enableSchemaCache]] must be set true. | ||
$enableSlaves | whether to enable read/write splitting by using [[slaves]] to read data. Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes. | ||
$masterConfig | the configuration that should be merged with every master configuration listed in [[masters]]. For example, php [ 'username' => 'master', 'password' => 'master', 'attributes' => [ use a smaller connection timeout PDO::ATTR_TIMEOUT => 10, ], ] | ||
$masters | list of master connection configurations. Each configuration is used to create a master DB connection. When Connection::open is called, one of these configurations will be chosen and used to create a DB connection which will be used by this object. Note that when this property is not empty, the connection setting (e.g. "dsn", "username") of this object will be ignored. | ||
$password | the password for establishing DB connection. Defaults to null meaning no password to use. | ||
$pdo | the PHP PDO instance associated with this DB connection. This property is mainly managed by Connection::open and Connection::close methods. When a DB connection is active, this property will represent a PDO instance; otherwise, it will be null. | ||
$pdoClass | Custom PDO wrapper class. If not set, it will use [[PDO]] or PDO when MSSQL is used. | ||
$queryCache | the cache object or the ID of the cache application component that is used for query caching. | ||
$queryCacheDuration | the default number of seconds that query results can remain valid in cache. Defaults to 3600, meaning 3600 seconds, or one hour. Use 0 to indicate that the cached data will never expire. The value of this property will be used when Connection::cache is called without a cache duration. | ||
$schemaCache | the cache object or the ID of the cache application component that is used to cache the table metadata. | ||
$schemaCacheDuration | number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will never expire. | ||
$schemaCacheExclude | list of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema prefix, if any. Do not quote the table names. | ||
$schemaMap | mapping between PDO driver names and Schema classes. The keys of the array are PDO driver names while the values the corresponding schema class name or configuration. Please refer to [[Yii::createObject()]] for details on how to specify a configuration. This property is mainly used by Connection::getSchema when fetching the database schema information. You normally do not need to set this property unless you want to use your own Schema class to support DBMS that is not supported by Yii. | ||
$serverRetryInterval | the retry interval in seconds for dead servers listed in [[masters]] and [[slaves]]. This is used together with [[serverStatusCache]]. | ||
$serverStatusCache | 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]]. This is used only when read/write splitting is enabled or [[masters]] is not empty. | ||
$slaveConfig | the configuration that should be merged with every slave configuration listed in [[slaves]]. For example, php [ 'username' => 'slave', 'password' => 'slave', 'attributes' => [ use a smaller connection timeout PDO::ATTR_TIMEOUT => 10, ], ] | ||
$slaves | list of slave connection configurations. Each configuration is used to create a slave DB connection. When [[enableSlaves]] is true, one of these configurations will be chosen and used to create a DB connection for performing read queries only. | ||
$tablePrefix | the common prefix or suffix for table names. If a table name is given as {{%TableName}}, then the percentage character % will be replaced with this property value. For example, {{%post}} becomes {{tbl_post}}. | ||
$username | the username for establishing DB connection. Defaults to null meaning no username to use. |
Method | Description | |
---|---|---|
__sleep ( ) : array | Close the connection before serializing. | |
beginTransaction ( string | null $isolationLevel = null ) : |
Starts a transaction. | |
cache ( callable $callable, integer $duration = null, yii\caching\Dependency $dependency = null ) : mixed | Uses query cache for the queries performed with the callable. | |
close ( ) | Closes the currently active DB connection. | |
createCommand ( string $sql = null, array $params = [] ) : |
Creates a command for execution. | |
getDriverName ( ) : string | Returns the name of the DB driver. Based on the the current [[dsn]], in case it was not set explicitly by an end user. | |
getIsActive ( ) : boolean | Returns a value indicating whether the DB connection is established. | |
getLastInsertID ( string $sequenceName = '' ) : string | Returns the ID of the last inserted row or sequence value. | |
getMasterPdo ( ) : PDO | Returns the PDO instance for the currently active master connection. | |
getQueryBuilder ( ) : |
Returns the query builder for the current DB connection. | |
getQueryCacheInfo ( integer $duration, yii\caching\Dependency $dependency ) : array | Returns the current query cache information. | |
getSchema ( ) : |
Returns the schema information for the database opened by this connection. | |
getSlave ( boolean $fallbackToMaster = true ) : |
Returns the currently active slave connection. | |
getSlavePdo ( boolean $fallbackToMaster = true ) : PDO | Returns the PDO instance for the currently active slave connection. | |
getTableSchema ( string $name, boolean $refresh = false ) : yii\db\TableSchema | Obtains the schema information for the named table. | |
getTransaction ( ) : |
Returns the currently active transaction. | |
noCache ( callable $callable ) : mixed | Disables query cache temporarily. | |
open ( ) | Establishes a DB connection. | |
quoteColumnName ( string $name ) : string | Quotes a column name for use in a query. | |
quoteSql ( string $sql ) : string | Processes a SQL statement by quoting table and column names that are enclosed within double brackets. | |
quoteTableName ( string $name ) : string | Quotes a table name for use in a query. | |
quoteValue ( string $value ) : string | Quotes a string value for use in a query. | |
setDriverName ( string $driverName ) | Changes the current driver name. | |
transaction ( callable $callback, string | null $isolationLevel = null ) : mixed | Executes callback provided in a transaction. | |
useMaster ( callable $callback ) : mixed | Executes the provided callback by using the master connection. |
Method | Description | |
---|---|---|
createPdoInstance ( ) : PDO | Creates the PDO instance. | |
initConnection ( ) | Initializes the DB connection. | |
openFromPool ( array $pool, array $sharedConfig ) : |
Opens the connection to a server in the pool. |
public beginTransaction ( string | null $isolationLevel = null ) : |
||
$isolationLevel | string | null | The isolation level to use for this transaction. See [[Transaction::begin()]] for details. |
return | the transaction initiated |
public cache ( callable $callable, integer $duration = null, yii\caching\Dependency $dependency = null ) : mixed | ||
$callable | callable | a PHP callable that contains DB queries which will make use of query cache. The signature of the callable is `function (Connection $db)`. |
$duration | integer | the number of seconds that query results can remain valid in the cache. If this is not set, the value of [[queryCacheDuration]] will be used instead. Use 0 to indicate that the cached data will never expire. |
$dependency | yii\caching\Dependency | the cache dependency associated with the cached query results. |
return | mixed | the return result of the callable |
public createCommand ( string $sql = null, array $params = [] ) : |
||
$sql | string | the SQL statement to be executed |
$params | array | the parameters to be bound to the SQL statement |
return | the DB command |
protected createPdoInstance ( ) : PDO | ||
return | PDO | the pdo instance |
public getDriverName ( ) : string | ||
return | string | name of the DB driver |
public getIsActive ( ) : boolean | ||
return | boolean | whether the DB connection is established |
public getLastInsertID ( string $sequenceName = '' ) : string | ||
$sequenceName | string | name of the sequence object (required by some DBMS) |
return | string | the row ID of the last row inserted, or the last value retrieved from the sequence object |
public getMasterPdo ( ) : PDO | ||
return | PDO | the PDO instance for the currently active master connection. |
public getQueryBuilder ( ) : |
||
return | the query builder for the current DB connection. |
public getQueryCacheInfo ( integer $duration, yii\caching\Dependency $dependency ) : array | ||
$duration | integer | the preferred caching duration. If null, it will be ignored. |
$dependency | yii\caching\Dependency | the preferred caching dependency. If null, it will be ignored. |
return | array | the current query cache information, or null if query cache is not enabled. |
public getSchema ( ) : |
||
return | the schema information for the database opened by this connection. |
public getSlave ( boolean $fallbackToMaster = true ) : |
||
$fallbackToMaster | boolean | whether to return a master connection in case there is no slave connection available. |
return | the currently active slave connection. Null is returned if there is slave available and `$fallbackToMaster` is false. |
public getSlavePdo ( boolean $fallbackToMaster = true ) : PDO | ||
$fallbackToMaster | boolean | whether to return a master PDO in case none of the slave connections is available. |
return | PDO | the PDO instance for the currently active slave connection. Null is returned if no slave connection is available and `$fallbackToMaster` is false. |
public getTableSchema ( string $name, boolean $refresh = false ) : yii\db\TableSchema | ||
$name | string | table name. |
$refresh | boolean | whether to reload the table schema even if it is found in the cache. |
return | yii\db\TableSchema | table schema information. Null if the named table does not exist. |
public getTransaction ( ) : |
||
return | the currently active transaction. Null if no active transaction. |
protected initConnection ( ) |
public open ( ) |
protected openFromPool ( array $pool, array $sharedConfig ) : |
||
$pool | array | the list of connection configurations in the server pool |
$sharedConfig | array | the configuration common to those given in `$pool`. |
return | the opened DB connection, or null if no server is available |
public quoteColumnName ( string $name ) : string | ||
$name | string | column name |
return | string | the properly quoted column name |
public quoteTableName ( string $name ) : string | ||
$name | string | table name |
return | string | the properly quoted table name |
public quoteValue ( string $value ) : string | ||
$value | string | string to be quoted |
return | string | the properly quoted string |
public setDriverName ( string $driverName ) | ||
$driverName | string | name of the DB driver |
public transaction ( callable $callback, string | null $isolationLevel = null ) : mixed | ||
$callback | callable | a valid PHP callback that performs the job. Accepts connection instance as parameter. |
$isolationLevel | string | null | The isolation level to use for this transaction. See [[Transaction::begin()]] for details. |
return | mixed | result of callback function |
public $attributes |
public $charset |
public $commandClass |
public $dsn |
public $emulatePrepare |
public $enableQueryCache |
public $enableSavepoint |
public $enableSchemaCache |
public $enableSlaves |
public $masterConfig |
public $masters |
public $password |
public $pdo |
public $pdoClass |
public $queryCache |
public $queryCacheDuration |
public $schemaCache |
public $schemaCacheDuration |
public $schemaCacheExclude |
public $schemaMap |
public $serverRetryInterval |
public $serverStatusCache |
public $slaveConfig |
public $slaves |
public $tablePrefix |