CDbConnection

Namedb
ClassCDbConnection
Location/home/vhosts/app-manager.eu5.org/framework/db/CDbConnection.php
CDbConnection represents a connection to a database.

CDbConnection works together with {@link CDbCommand}, {@link CDbDataReader} and {@link CDbTransaction} to provide data access to various DBMS in a common set of APIs. They are a thin wrapper of the {@link http://www.php.net/manual/en/ref.pdo.php PDO} PHP extension. To establish a connection, set {@link setActive active} to true after specifying {@link connectionString}, {@link username} and {@link password}. The following example shows how to create a CDbConnection instance and establish the actual connection:
$connection=new CDbConnection($dsn,$username,$password);
$connection->active=true;
After the DB connection is established, one can execute an SQL statement like the following:
$command=$connection->createCommand($sqlStatement);
$command->execute();   // a non-query SQL statement execution
// or execute an SQL query and fetch the result set
$reader=$command->query();

// each $row is an array representing a row of data
foreach($reader as $row) ...
One can do prepared SQL execution and bind parameters to the prepared SQL:
$command=$connection->createCommand($sqlStatement);
$command->bindParam($name1,$value1);
$command->bindParam($name2,$value2);
$command->execute();
To use transaction, do like the following:
$transaction=$connection->beginTransaction();
try
{
   $connection->createCommand($sql1)->execute();
   $connection->createCommand($sql2)->execute();
   //.... other SQL executions
   $transaction->commit();
}
catch(Exception $e)
{
   $transaction->rollback();
}
CDbConnection also provides a set of methods to support setting and querying of certain DBMS attributes, such as {@link getNullConversion nullConversion}. Since CDbConnection implements the interface IApplicationComponent, it can be used as an application component and be configured in application configuration, like the following,
array(
    'components'=>array(
        'db'=>array(
            'class'=>'CDbConnection',
            'connectionString'=>'sqlite:path/to/dbfile',
        ),
    ),
)

Options

Displaying 1-10 of 26 results.
NameValueDescriptionType
connectionString'sqlite:/home/vhosts/app-manager.eu5.org/protected/runtime/config/../data/testdrive.db'The Data Source Name, or DSN, contains the information required to connect to the database. @see http://www.php.net/manual/en/function.PDO-construct.php Note that if you're using GBK or BIG5 then it's highly recommended to update to PHP 5.3.6+ and to specify charset via DSN like 'mysql:dbname=mydatabase;host=127.0.0.1;charset=GBK;'.string
username''The username for establishing DB connection. Defaults to empty string.string
password''The password for establishing DB connection. Defaults to empty string.string
schemaCachingDuration0Number of seconds that table metadata can remain valid in cache. Use 0 or negative value to indicate not caching schema. If greater than 0 and the primary cache is enabled, the table metadata will be cached. @see schemaCachingExcludeinteger
schemaCachingExcludearray()List of tables whose metadata should NOT be cached. Defaults to empty array. @see schemaCachingDurationarray
schemaCacheID'cache'The ID of the cache application component that is used to cache the table metadata. Defaults to 'cache' which refers to the primary cache application component. Set this property to false if you want to disable caching table metadata.string
queryCachingDuration0Number of seconds that query results can remain valid in cache. Use 0 or negative value to indicate not caching query results (the default behavior). In order to enable query caching, this property must be a positive integer and {@link queryCacheID} must point to a valid cache component ID. The method {@link cache()} is provided as a convenient way of setting this property and {@link queryCachingDependency} on the fly. @see cache @see queryCachingDependency @see queryCacheID @since 1.1.7integer
queryCachingDependencynullThe dependency that will be used when saving query results into cache. @see queryCachingDuration @since 1.1.7CCacheDependency
queryCachingCount0The number of SQL statements that need to be cached next. If this is 0, then even if query caching is enabled, no query will be cached. Note that each time after executing a SQL statement (whether executed on DB server or fetched from query cache), this property will be reduced by 1 until 0. @since 1.1.7integer
queryCacheID'cache'The ID of the cache application component that is used for query caching. Defaults to 'cache' which refers to the primary cache application component. Set this property to false if you want to disable query caching. @since 1.1.7string