Connection works together with Command, DataReader and Transaction
to provide data access to various DBMS in a common set of APIs. They are a thin wrapper
of the [[PDO PHP extension]](http://www.php.net/manual/en/ref.pdo.php).
Connection supports database replication and read-write splitting. In particular, a Connection component
can be configured with multiple [[masters]] and [[slaves]]. It will do load balancing and failover by choosing
appropriate servers. It will also automatically direct read operations to the slaves and write operations to
the masters.
To establish a DB connection, set [[dsn]], [[username]] and [[password]], and then
call Connection::open to be true.
The following example shows how to create a Connection instance and establish
the DB connection:
~~~
$connection = new \yii\db\Connection([
'dsn' => $dsn,
'username' => $username,
'password' => $password,
]);
$connection->open();
~~~
After the DB connection is established, one can execute SQL statements like the following:
~~~
$command = $connection->createCommand('SELECT * FROM post');
$posts = $command->queryAll();
$command = $connection->createCommand('UPDATE post SET status=1');
$command->execute();
~~~
One can also do prepared SQL execution and bind parameters to the prepared SQL.
When the parameters are coming from user input, you should use this approach
to prevent SQL injection attacks. The following is an example:
~~~
$command = $connection->createCommand('SELECT * FROM post WHERE id=:id');
$command->bindValue(':id', $_GET['id']);
$post = $command->query();
~~~
For more information about how to perform various DB queries, please refer to Command.
If the underlying DBMS supports transactions, you can perform transactional SQL queries
like the following:
~~~
$transaction = $connection->beginTransaction();
try {
$connection->createCommand($sql1)->execute();
$connection->createCommand($sql2)->execute();
... executing other SQL statements ...
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
}
~~~
You also can use shortcut for the above like the following:
~~~
$connection->transaction(function() {
$order = new Order($customer);
$order->save();
$order->addItems($items);
});
~~~
If needed you can pass transaction isolation level as a second parameter:
~~~
$connection->transaction(function(Connection $db) {
return $db->...
}, Transaction::READ_UNCOMMITTED);
~~~
Connection is often used as an application component and configured in the application
configuration like the following:
~~~
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
~~~