CDbConnection
Name | db |
---|---|
Class | CDbConnection |
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:
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 21-26 of 26 results.
Name | Value | Description | Type |
---|---|---|---|
active | null | Open or close the DB connection. whether to open or close DB connection @throws CException if connection fails | boolean |
columnCase | null | Sets the case of the column names. the case of the column names @see http://www.php.net/manual/en/pdo.setattribute.php | mixed |
nullConversion | null | Sets how the null and empty strings are converted. how the null and empty strings are converted @see http://www.php.net/manual/en/pdo.setattribute.php | mixed |
autoCommit | null | Sets whether creating or updating a DB record will be automatically committed. Some DBMS (such as sqlite) may not support this feature. whether creating or updating a DB record will be automatically committed. | boolean |
persistent | null | Sets whether the connection is persistent or not. Some DBMS (such as sqlite) may not support this feature. whether the connection is persistent or not | boolean |
attributes | null | Sets a set of attributes on the database connection. attributes (name=>value) to be set. @see setAttribute @since 1.1.7 | array |
Go to page: