db-dao.md 19.9 KB
Newer Older
1
Database Access Objects
2 3
===============

4
> Note: This section is under development.
Qiang Xue committed
5

6 7 8 9
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.

By default, Yii supports the following DBMS:
10 11

- [MySQL](http://www.mysql.com/)
12
- [MariaDB](https://mariadb.com/)
13 14
- [SQLite](http://sqlite.org/)
- [PostgreSQL](http://www.postgresql.org/)
15 16
- [CUBRID](http://www.cubrid.org/): version 9.3 or higher. (Note that due to a [bug](http://jira.cubrid.org/browse/APIS-658) in
  the cubrid PDO extension, quoting of values will not work, so you need CUBRID 9.3 as the client as well as the server)
17
- [Oracle](http://www.oracle.com/us/products/database/overview/index.html)
18
- [MSSQL](https://www.microsoft.com/en-us/sqlserver/default.aspx): version 2005 or higher.
19

20

21 22 23
Configuration
-------------

24
To start interacting with a database (using DAO or otherwise), you need to configure the application's database connection component. The Data Source Name (DSN) configures to which database application and specific database the application should connect:
25 26

```php
Alexander Makarov committed
27
return [
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=mydatabase', // MySQL, MariaDB
            //'dsn' => 'sqlite:/path/to/database/file', // SQLite
            //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL
            //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID
            //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase', // MS SQL Server, sqlsrv driver
            //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver
            //'dsn' => 'mssql:host=localhost;dbname=mydatabase', // MS SQL Server, mssql driver
            //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
    // ...
Alexander Makarov committed
47
];
48
```
49

50 51 52 53 54
Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) for more details
on the format of the DSN string. Refer to [[yii\db\Connection]] for the full list of properties you can configure in the class.

A peculiarity exists when you want to work with the database through the `ODBC` layer. When using `ODBC`, the
connection `DSN` doesn't uniquely indicate what database type is being used. For that reason, you have to override the
55 56 57 58
`driverName` property of [[yii\db\Connection]] class to disambiguate that:

```php
'db' => [
59 60 61 62 63
    'class' => 'yii\db\Connection',
    'driverName' => 'mysql',
    'dsn' => 'odbc:Driver={MySQL};Server=localhost;Database=test',
    'username' => 'root',
    'password' => '',
64 65 66
],
```

67
Overriding `driverName` is not necessary when not going through ODBC. 
68

69
Given the "db" component's configuration in the application, you can access the database connection using:
70 71 72 73 74

```php
$connection = \Yii::$app->db;
```

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
You can define more
than one connection component:

```php
return [
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=mydatabase', 
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        'secondDb' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'sqlite:/path/to/database/file', 
        ],
    ],
    // ...
];
```

Now you can use both database connections at the same time as needed:
100 101 102 103 104 105

```php
$primaryConnection = \Yii::$app->db;
$secondaryConnection = \Yii::$app->secondDb;
```

106
If you don't want to define the connection as an [application component](structure-application-components.md), you can instantiate it directly:
107 108

```php
Alexander Makarov committed
109
$connection = new \yii\db\Connection([
110
    'dsn' => $dsn,
111 112
    'username' => $username,
    'password' => $password,
Alexander Makarov committed
113
]);
114 115 116
$connection->open();
```

117
> 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:
118 119 120
>
```php
return [
121 122 123 124 125 126 127 128 129 130 131 132
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            // ...
            'on afterOpen' => function($event) {
                $event->sender->createCommand("SET time_zone = 'UTC'")->execute();
            }
        ],
    ],
    // ...
133 134 135
];
```

136 137
Executing Basic SQL Queries
---------------------------
138

139
Once you have a database connection instance, you can execute SQL queries using [[yii\db\Command]].
140

141
### Running SELECT Queries
142

143
When the query to be executed returns a set of rows, you'll use `queryAll`:
144 145

```php
146
$command = $connection->createCommand('SELECT * FROM post');
147 148 149
$posts = $command->queryAll();
```

150
When the query to be executed only returns a single row, you'll use `queryOne`:
151 152

```php
153
$command = $connection->createCommand('SELECT * FROM post WHERE id=1');
154
$post = $command->queryOne();
155 156
```

157
When the query returns multiple rows but only one column, you'll use `queryColumn`:
158 159

```php
160
$command = $connection->createCommand('SELECT title FROM post');
161 162 163
$titles = $command->queryColumn();
```

164
When the query only returns a scalar value, you'll use `queryScalar`:
165 166

```php
167
$command = $connection->createCommand('SELECT COUNT(*) FROM post');
168 169 170
$postCount = $command->queryScalar();
```

171
### Running Queries That Don't Return Values
172

173
If SQL executed doesn't return any data--for example, INSERT, UPDATE, and DELETE, you can use command's `execute` method:
174 175

```php
176
$command = $connection->createCommand('UPDATE post SET status=1 WHERE id=1');
177 178 179
$command->execute();
```

180 181 182
Alternatively, you can use dedicated `insert`, `update`, and `delete` method. These methods will properly quote table and column names used in your query, and you only need to provide the necessary values:

[[Ought to put a link to the reference docs here.]]
183 184 185

```php
// INSERT
186
$connection->createCommand()->insert('user', [
187 188
    'name' => 'Sam',
    'age' => 30,
Alexander Makarov committed
189
])->execute();
190 191

// INSERT multiple rows at once
192
$connection->createCommand()->batchInsert('user', ['name', 'age'], [
193 194 195
    ['Tom', 30],
    ['Jane', 20],
    ['Linda', 25],
Alexander Makarov committed
196
])->execute();
197 198

// UPDATE
199
$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
200 201

// DELETE
202
$connection->createCommand()->delete('user', 'status = 0')->execute();
203 204
```

205
Quoting Table and Column Names
206 207
------------------------------

Larry Ullman committed
208
To make column and table names safe to use in queries, you can have Yii properly quote them for you:
209 210

```php
Alexander Makarov committed
211
$sql = "SELECT COUNT([[$column]]) FROM {{table}}";
212 213 214
$rowCount = $connection->createCommand($sql)->queryScalar();
```

Larry Ullman committed
215
In the code above, `[[$column]]` will be converted to properly quoted column name, while `{{table}}` will be converted to a properly-quoted table name.
216

Larry Ullman committed
217
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:
218 219

```php
Alexander Makarov committed
220
$sql = "SELECT COUNT([[$column]]) FROM {{%table}}";
221 222 223
$rowCount = $connection->createCommand($sql)->queryScalar();
```

Larry Ullman committed
224
The code above will result in selecting from `tbl_table`, if you have table prefix configured like so:
225 226 227 228 229 230 231 232 233 234 235 236 237 238

```php
return [
    // ...
    'components' => [
        // ...
        'db' => [
            // ...
            'tablePrefix' => 'tbl_',
        ],
    ],
];
```

239 240
The alternative is to quote table and column names manually using [[yii\db\Connection::quoteTableName()]] and
[[yii\db\Connection::quoteColumnName()]]:
241 242 243 244 245 246 247

```php
$column = $connection->quoteColumnName($column);
$table = $connection->quoteTableName($table);
$sql = "SELECT COUNT($column) FROM $table";
$rowCount = $connection->createCommand($sql)->queryScalar();
```
248

Larry Ullman committed
249
Using Prepared Statements
250 251
-------------------

252
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:
253 254

```php
255
$command = $connection->createCommand('SELECT * FROM post WHERE id=:id');
256
$command->bindValue(':id', $_GET['id']);
Manop Kongoon committed
257
$post = $command->queryOne();
258 259
```

260
Another purpose for prepared statements (aside from improved security) is the ability to execute a query multiple times while preparing it only once:
261 262

```php
263
$command = $connection->createCommand('DELETE FROM post WHERE id=:id');
264 265 266 267 268 269 270 271 272
$command->bindParam(':id', $id);

$id = 1;
$command->execute();

$id = 2;
$command->execute();
```

273 274 275 276
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. 

Performing Transactions
-----------------------
277

278 279
When running multiple, related queries in a sequence, you may need to wrap them in a transaction to
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
280 281 282
cases but also for advanced usage when you need to define isolation levels.

The following code shows a simple pattern that all code that uses transactional queries should follow:
283 284 285 286

```php
$transaction = $connection->beginTransaction();
try {
287
    $connection->createCommand($sql1)->execute();
288
    $connection->createCommand($sql2)->execute();
289 290
    // ... executing other SQL statements ...
    $transaction->commit();
291
} catch(\Exception $e) {
292
    $transaction->rollBack();
293
    throw $e;
294 295 296
}
```

297 298 299 300 301 302
The first line starts a new transaction using the [[yii\db\Connection::beginTransaction()|beginTransaction()]]-method of the database connection
object. The transaction itself is represented by a [[yii\db\Transaction]] object stored in `$transaction`.
We wrap the execution of all queries in a try-catch-block to be able to handle errors.
We call [[yii\db\Transaction::commit()|commit()]] on success to commit the transaction and
[[yii\db\Transaction::rollBack()|rollBack()]] in case of an error. This will revert the effect of all queries
that have been executed inside of the transaction.
303 304
`throw $e` is used to re-throw the exception in case we can not handle the error ourselves and delegate it
to some other code or the yii error handler.
305 306

It is also possible to nest multiple transactions, if needed:
307 308 309 310 311

```php
// outer transaction
$transaction1 = $connection->beginTransaction();
try {
312 313 314 315 316 317 318 319 320 321 322 323
    $connection->createCommand($sql1)->execute();

    // inner transaction
    $transaction2 = $connection->beginTransaction();
    try {
        $connection->createCommand($sql2)->execute();
        $transaction2->commit();
    } catch (Exception $e) {
        $transaction2->rollBack();
    }

    $transaction1->commit();
324
} catch (Exception $e) {
325
    $transaction1->rollBack();
326 327 328
}
```

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
Note that your DBMS should have support for Savepoints for this to work as expected.
The above code will work for any DBMS but transactional safety is only guaranteed if
the underlying DBMS supports it.

Yii also supports setting [isolation levels] for your transactions.
When beginning a transaction it will run in the default isolation level set by you database system.
You can specifying an isolation level explicitly when starting a transaction:

```php
$transaction = $connection->beginTransaction(\yii\db\Transaction::REPEATABLE_READ);
```

Yii provides four constants for the most common isolation levels:

- [[\yii\db\Transaction::READ_UNCOMMITTED]] - the weakest level, Dirty reads, Non-repeatable reads and Phantoms may occur.
- [[\yii\db\Transaction::READ_COMMITTED]] - avoid Dirty reads.
- [[\yii\db\Transaction::REPEATABLE_READ]] - avoid Dirty reads and Non-repeatable reads.
- [[\yii\db\Transaction::SERIALIZABLE]] - the strongest level, avoids all of the above named problems.

You may use the constants named above but you can also use a string that represents a valid syntax that can be
used in your DBMS following `SET TRANSACTION ISOLATION LEVEL`. For postgres this could be for example
`SERIALIZABLE READ ONLY DEFERRABLE`.

352 353 354 355 356
Note that some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions
may get the same isolation level even if you did not specify any. When using this feature
you may need to set the isolation level for all transactions explicitly to avoid conflicting settings.
At the time of this writing affected DBMS are MSSQL and SQLite.

357
> Note: SQLite only supports two isolation levels, so you can only use `READ UNCOMMITTED` and `SERIALIZABLE`.
358
Usage of other levels will result in an exception to be thrown.
359

360
> Note: PostgreSQL does not allow setting the isolation level before the transaction starts so you can not
361 362
specify the isolation level directly when starting the transaction.
You have to call [[yii\db\Transaction::setIsolationLevel()]] in this case after the transaction has started.
363 364 365

[isolation levels]: http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels

366

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
Replication and Read-Write Splitting
------------------------------------

Many DBMS support [database replication](http://en.wikipedia.org/wiki/Replication_(computing)#Database_replication)
to get better database availability and faster server response time. With database replication, data are replicated
from the so-called *master servers* to *slave servers*. All writes and updates must take place on the master servers,
while reads may take place on the slave servers.

To take advantage of database replication and achieve read-write splitting, you can configure a [[yii\db\Connection]]
component like the following:

```php
[
    'class' => 'yii\db\Connection',

    // configuration for the master
    'dsn' => 'dsn for master server',
    'username' => 'master',
    'password' => '',

    // common configuration for slaves
    'slaveConfig' => [
        'username' => 'slave',
        'password' => '',
        'attributes' => [
            // use a smaller connection timeout
            PDO::ATTR_TIMEOUT => 10,
        ],
    ],

    // list of slave configurations
    'slaves' => [
        ['dsn' => 'dsn for slave server 1'],
        ['dsn' => 'dsn for slave server 2'],
        ['dsn' => 'dsn for slave server 3'],
        ['dsn' => 'dsn for slave server 4'],
    ],
]
```

The above configuration specifies a setup with a single master and multiple slaves. One of the slaves will
be connected and used to perform read queries, while the master will be used to perform write queries.
Such read-write splitting is accomplished automatically with this configuration. For example,

```php
// create a Connection instance using the above configuration
$db = Yii::createObject($config);

// query against one of the slaves
$rows = $db->createCommand('SELECT * FROM user LIMIT 10')->queryAll();

// query against the master
$db->createCommand("UPDATE user SET username='demo' WHERE id=1")->execute();
```

> Info: Queries performed by calling [[yii\db\Command::execute()]] are considered as write queries, while
  all other queries done through one of the "query" method of [[yii\db\Command]] are read queries.
  You can get the currently active slave connection via `$db->slave`.

The `Connection` component supports load balancing and failover about slaves.
When performing a read query for the first time, the `Connection` component will randomly pick a slave and
try connecting to it. If the slave is found "dead", it will try another one. If none of the slaves is available,
it will connect to the master. By configuring a [[yii\db\Connection::serverStatusCache|server status cache]],
a "dead" server can be remembered so that it will not be tried again during a
[[yii\db\Connection::serverRetryInterval|certain period of time]].

> Info: In the above configuration, a connection timeout of 10 seconds is specified for every slave.
  This means if a slave cannot be reached in 10 seconds, it is considered as "dead". You can adjust this parameter
  based on your actual environment.


You can also configure multiple masters with multiple slaves. For example,


```php
[
    'class' => 'yii\db\Connection',

    // common configuration for masters
    'masterConfig' => [
        'username' => 'master',
        'password' => '',
        'attributes' => [
            // use a smaller connection timeout
            PDO::ATTR_TIMEOUT => 10,
        ],
    ],

    // list of master configurations
    'masters' => [
        ['dsn' => 'dsn for master server 1'],
        ['dsn' => 'dsn for master server 2'],
    ],

    // common configuration for slaves
    'slaveConfig' => [
        'username' => 'slave',
        'password' => '',
        'attributes' => [
            // use a smaller connection timeout
            PDO::ATTR_TIMEOUT => 10,
        ],
    ],

    // list of slave configurations
    'slaves' => [
        ['dsn' => 'dsn for slave server 1'],
        ['dsn' => 'dsn for slave server 2'],
        ['dsn' => 'dsn for slave server 3'],
        ['dsn' => 'dsn for slave server 4'],
    ],
]
```

The above configuration specifies two masters and four slaves. The `Connection` component also supports
load balancing and failover about masters, like that about slaves. A difference is that in case none of
the masters is available, an exception will be thrown.

> Note: When you use the [[yii\db\Connection::masters|masters]] property to configure one or multiple
  masters, all other properties for specifying a database connection (e.g. `dsn`, `username`, `password`)
  with the `Connection` object itself will be ignored.


By default, transactions use the master connection. And within a transaction, all DB operations will use
the master connection. For example,

```php
// the transaction is started on the master connection
$transaction = $db->beginTransaction();

try {
    // both queries are performed against the master
    $rows = $db->createCommand('SELECT * FROM user LIMIT 10')->queryAll();
    $db->createCommand("UPDATE user SET username='demo' WHERE id=1")->execute();

    $transaction->commit();
} catch(\Exception $e) {
    $transaction->rollBack();
    throw $e;
}
```

If you want to start a transaction with the slave connection, you should explicitly do so, like the following:

```php
$transaction = $db->slave->beginTransaction();
```

Sometimes, you may want to force using the master connection to perform a read query. This can be achieved
with the `useMaster()` method:

```php
$rows = $db->useMaster(function ($db) {
    return $db->createCommand('SELECT * FROM user LIMIT 10')->queryAll();
});
```

Qiang Xue committed
524 525
You may also directly set `$db->enableSlaves` to be false to direct all queries to the master connection.

526

527 528 529 530 531
Working with database schema
----------------------------

### Getting schema information

532
You can get a [[yii\db\Schema]] instance like the following:
533 534 535 536 537 538 539 540 541 542 543

```php
$schema = $connection->getSchema();
```

It contains a set of methods allowing you to retrieve various information about the database:

```php
$tables = $schema->getTableNames();
```

544
For the full reference check [[yii\db\Schema]].
545 546 547

### Modifying schema

548
Aside from basic SQL queries [[yii\db\Command]] contains a set of methods allowing to modify database schema:
549 550 551 552 553 554 555 556 557 558

- createTable, renameTable, dropTable, truncateTable
- addColumn, renameColumn, dropColumn, alterColumn
- addPrimaryKey, dropPrimaryKey
- addForeignKey, dropForeignKey
- createIndex, dropIndex

These can be used as follows:

```php
559
// CREATE TABLE
560
$connection->createCommand()->createTable('post', [
561 562 563
    'id' => 'pk',
    'title' => 'string',
    'text' => 'text',
Alexander Makarov committed
564
]);
565 566
```

567
For the full reference check [[yii\db\Command]].