Yii includes a database access layer built on top of PHP's [PDO](http://www.php.net/manual/en/book.pdo.php). The database access objects (DAO) interface provides a
uniform API, and solves some inconsistencies that exist between different database applications. Whereas Active Record provides database interactions through models, and the Query Builder assists in composing dynamic queries, DAO is a simple and efficient way to execute straight SQL on your database. You'll want to use DAO when the query to be run is expensive and/or no application models--and their corresponding business logic--are required.
データベースアクセスオブジェクト (DAO) のインタフェイスは、統一された API を提供し、さまざまなデータベース製品間に存在する不統一をいくらか解決します。
DB connections in a single application. Simply assign different IDs to them in the application configuration:
一つのアプリケーションで複数の DB 接続を構成することも出来ます。
アプリケーションの構成情報において、それらに別々の ID を割り当てるだけのことです。
```php
```php
return[
return[
...
@@ -88,14 +92,14 @@ return [
...
@@ -88,14 +92,14 @@ return [
];
];
```
```
Now you can use both database connections at the same time as needed:
これで、必要に応じて両方のデータベース接続を同時に使用することが出来ます。
```php
```php
$primaryConnection=\Yii::$app->db;
$primaryConnection=\Yii::$app->db;
$secondaryConnection=\Yii::$app->secondDb;
$secondaryConnection=\Yii::$app->secondDb;
```
```
If you don't want to define the connection as an [application component](structure-application-components.md), you can instantiate it directly:
DB 接続を [アプリケーションコンポーネント](structure-application-components.md) として定義したくない場合は、インスタンスを直接に作成することも出来ます。
```php
```php
$connection=new\yii\db\Connection([
$connection=new\yii\db\Connection([
...
@@ -106,7 +110,7 @@ $connection = new \yii\db\Connection([
...
@@ -106,7 +110,7 @@ $connection = new \yii\db\Connection([
$connection->open();
$connection->open();
```
```
> Tip: If you need to execute an SQL query immediately after establishing a connection (e.g., to set the timezone or character set), you can add the following to your application configuration file:
$command=$connection->createCommand('UPDATE post SET status=1 WHERE id=1');
$command=$connection->createCommand('UPDATE post SET status=1 WHERE id=1');
$command->execute();
$command->execute();
```
```
Alternatively, you can use the dedicated `insert`, `update`, and `delete` methods. These methods will properly quote table and column names used in your query, and you only need to provide the necessary values:
In the code above, `[[$column]]` will be converted to a properly quoted column name, while `{{table}}` will be converted to a properly quoted table name.
There's a special variant on this syntax specific to tablenames: `{{%Y}}` automatically appends the application's table prefix to the provided value, if a table prefix has been set:
To securely pass query parameters to your queries, you should make use of prepared statements. First, create a named placeholder in your query (using the syntax `:placeholder`). Then bind the placeholder to a variable and execute the query:
$command=$connection->createCommand('DELETE FROM post WHERE id=:id');
$command=$connection->createCommand('DELETE FROM post WHERE id=:id');
...
@@ -262,23 +269,24 @@ $id = 2;
...
@@ -262,23 +269,24 @@ $id = 2;
$command->execute();
$command->execute();
```
```
Notice that you bind the placeholder to the variable before the execution, and then change the value of that variable before each subsequent execution (this is often done with loops). Executing queries in this manner can be vastly more efficient than running each query one at a time.
protect your data's integrity. Transactions allow you to write a series of queries such that they'll all succeed or have no effect whatsoever. Yii provides a simple interface to work with transactions in simple