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 11-20 of 26 results.
Name | Value | Description | Type |
---|---|---|---|
autoConnect | true | Whether the database connection should be automatically established the component is being initialized. Defaults to true. Note, this property is only effective when the CDbConnection object is used as an application component. | boolean |
charset | null | The charset used for database connection. The property is only used for MySQL and PostgreSQL databases. Defaults to null, meaning using default charset as specified by the database. 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 |
emulatePrepare | null | 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. Note, this property is only effective for PHP 5.1.3 or above. The default value is null, which will not change the ATTR_EMULATE_PREPARES value of PDO. | boolean |
enableParamLogging | false | Whether to log the values that are bound to a prepare SQL statement. Defaults to false. During development, you may consider setting this property to true so that parameter values bound to SQL statements are logged for debugging purpose. You should be aware that logging parameter values could be expensive and have significant impact on the performance of your application. | boolean |
enableProfiling | false | Whether to enable profiling the SQL statements being executed. Defaults to false. This should be mainly enabled and used during development to find out the bottleneck of SQL executions. | boolean |
tablePrefix | null | The default prefix for table names. Defaults to null, meaning no table prefix. By setting this property, any token like '{{tableName}}' in {@link CDbCommand::text} will be replaced by 'prefixTableName', where 'prefix' refers to this property value. @since 1.1.0 | string |
initSQLs | null | List of SQL statements that should be executed right after the DB connection is established. @since 1.1.1 | array |
driverMap | array ( 'pgsql' => 'CPgsqlSchema', 'mysqli' => 'CMysqlSchema', 'mysql' => 'CMysqlSchema', 'sqlite' => 'CSqliteSchema', 'sqlite2' => 'CSqliteSchema', 'mssql' => 'CMssqlSchema', 'dblib' => 'CMssqlSchema', 'sqlsrv' => 'CMssqlSchema', 'oci' => 'COciSchema', ) | Mapping between PDO driver and schema class name. A schema class can be specified using path alias. @since 1.1.6 | array |
pdoClass | 'PDO' | Custom PDO wrapper class. @since 1.1.8 | string |
behaviors | array() | The behaviors that should be attached to this component. The behaviors will be attached to the component when {@link init} is called. Please refer to {@link CModel::behaviors} on how to specify the value of this property. | array |