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

4 5 6
Built on top of [PDO](http://www.php.net/manual/en/book.pdo.php), Yii DAO (Database Access Objects) provides an
object-oriented API for accessing relational databases. It is the foundation for other more advanced database
access methods, including [query builder](db-query-builder.md) and [active record](db-active-record.md).
Qiang Xue committed
7

8 9 10
When using Yii DAO, you mainly need to deal with plain SQLs and PHP arrays. As a result, it is the most efficient 
way to access databases. However, because SQL syntax may vary for different databases, using Yii DAO also means 
you have to take extra effort to create a database-agnostic application.
11

12
Yii DAO supports the following databases out of box:
13 14

- [MySQL](http://www.mysql.com/)
15
- [MariaDB](https://mariadb.com/)
16 17
- [SQLite](http://sqlite.org/)
- [PostgreSQL](http://www.postgresql.org/)
18
- [CUBRID](http://www.cubrid.org/): version 9.3 or higher.
19
- [Oracle](http://www.oracle.com/us/products/database/overview/index.html)
Qiang Xue committed
20
- [MSSQL](https://www.microsoft.com/en-us/sqlserver/default.aspx): version 2008 or higher.
21

22

23
## Creating DB Connections <a name="creating-db-connections"></a>
24

25 26 27 28 29 30 31 32 33 34 35 36 37
To access a database, you first need to connect to it by creating an instance of [[yii\db\Connection]]:

```php
$db = new yii\db\Connection([
    'dsn' => 'mysql:host=localhost;dbname=example',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
]);
```

Because a DB connection often needs to be accessed in different places, a common practice is to configure it
in terms of an [application component](structure-application-components.md) like the following:
38 39

```php
Alexander Makarov committed
40
return [
41 42 43 44 45
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
46
            'dsn' => 'mysql:host=localhost;dbname=example',
47 48 49 50 51 52
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
    // ...
Alexander Makarov committed
53
];
54
```
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
You can then access the DB connection via the expression `Yii::$app->db`.

> Tip: You can configure multiple DB application components if your application needs to access multiple databases.

When configuring a DB connection, you should always specify its Data Source Name (DSN) via the [[yii\db\Connection::dsn|dsn]] 
property. The format of DSN varies for different databases. You may refer to Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) 
for more details. Below are some examples:
 
* MySQL, MariaDB: `mysql:host=localhost;dbname=mydatabase`
* SQLite: `sqlite:/path/to/database/file`
* PostgreSQL: `pgsql:host=localhost;port=5432;dbname=mydatabase`
* CUBRID: `cubrid:dbname=demodb;host=localhost;port=33000`
* MS SQL Server (via sqlsrv driver): `sqlsrv:Server=localhost;Database=mydatabase`
* MS SQL Server (via dblib driver): `dblib:host=localhost;dbname=mydatabase`
* MS SQL Server (via mssql driver): `mssql:host=localhost;dbname=mydatabase`
* Oracle: `oci:dbname=//localhost:1521/mydatabase`
72

Qiang Xue committed
73
Note that if you are connecting with a database via ODBC, you should configure the [[yii\db\Connection::driverName]]
74
property so that Yii can know the actual database type. For example,
75 76 77

```php
'db' => [
78 79 80 81 82
    'class' => 'yii\db\Connection',
    'driverName' => 'mysql',
    'dsn' => 'odbc:Driver={MySQL};Server=localhost;Database=test',
    'username' => 'root',
    'password' => '',
83 84 85
],
```

86 87
Besides the [[yii\db\Connection::dsn|dsn]] property, you often need to configure [[yii\db\Connection::username|username]]
and [[yii\db\Connection::password|password]]. Please refer to [[yii\db\Connection]] for the full list of configurable properties. 
88

89 90
> Info: When you create a DB connection instance, the actual connection to the database is not established until
  you execute the first SQL or you call the [[yii\db\Connection::open()|open()]] method explicitly.
91

92

93
## Executing SQL Queries <a name="executing-sql-queries"></a>
94

95 96 97 98 99
Once you have a database connection instance, you can execute a SQL query by taking the following steps:
 
1. Create a [[yii\db\Command]] with a plain SQL;
2. Bind parameters (optional);
3. Call one of the SQL execution methods in [[yii\db\Command]].
100

101 102
The following examples various ways of fetching data from a database:
 
103
```php
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
$db = new yii\db\Connection(...);

// return a set of rows. each row is an associative array of column names and values.
// an empty array is returned if no results
$posts = $db->createCommand('SELECT * FROM post')
            ->queryAll();

// return a single row (the first row)
// false is returned if no results
$post = $db->createCommand('SELECT * FROM post WHERE id=1')
           ->queryOne();

// return a single column (the first column)
// an empty array is returned if no results
$titles = $db->createCommand('SELECT title FROM post')
             ->queryColumn();

// return a scalar
// false is returned if no results
$count = $db->createCommand('SELECT COUNT(*) FROM post')
             ->queryScalar();
125 126
```

127 128 129 130 131
> Note: To preserve precision, the data fetched from databases are all represented as strings, even if the corresponding
  database column types are numerical.

> Tip: If you need to execute a SQL query right after establishing a connection (e.g., to set the timezone or character set), 
> you can do so in the [[yii\db\Connection::EVENT_AFTER_OPEN]] event handler. For example,
132 133 134
>
```php
return [
135 136 137 138 139 140 141
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            // ...
            'on afterOpen' => function($event) {
142
                // $event->sender refers to the DB connection
143 144 145 146 147
                $event->sender->createCommand("SET time_zone = 'UTC'")->execute();
            }
        ],
    ],
    // ...
148 149 150
];
```

151

152
### Binding Parameters <a name="binding-parameters"></a>
153

154 155
When creating a DB command from a SQL with parameters, you should almost always use the approach of binding parameters
to prevent SQL injection attacks. For example,
156 157

```php
158 159 160 161
$post = $db->createCommand('SELECT * FROM post WHERE id=:id AND status=:status')
           ->bindValue(':id', $_GET['id'])
           ->bindValue(':status', 1)
           ->queryOne();
162 163
```

164 165 166 167 168 169 170 171 172 173
In the SQL statement, you can embed one or multiple parameter placeholders (e.g. `:id` in the above example). A parameter
placeholder should be a string starting with a colon. You may then call one of the following parameter binding methods
to bind the parameter values:

* [[yii\db\Command::bindValue()|bindValue()]]: bind a single parameter value 
* [[yii\db\Command::bindValues()|bindValues()]]: bind multiple parameter values in one call
* [[yii\db\Command::bindParam()|bindParam()]]: similar to [[yii\db\Command::bindValue()|bindValue()]] but also
  support binding parameter references.

The following example shows alternative ways of binding parameters:
174 175

```php
176 177 178 179 180 181 182 183
$params = [':id' => $_GET['id'], ':status' => 1];

$post = $db->createCommand('SELECT * FROM post WHERE id=:id AND status=:status')
           ->bindValues($params)
           ->queryOne();
           
$post = $db->createCommand('SELECT * FROM post WHERE id=:id AND status=:status', $params)
           ->queryOne();
184 185
```

186 187 188
Parameter binding is implemented via [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php).
Besides preventing SQL injection attacks, it may also improve performance by preparing a SQL statement once and
executing it multiple times with different parameters. For example,
189 190

```php
191 192 193 194
$command = $db->createCommand('SELECT * FROM post WHERE id=:id');

$post1 = $command->bindValue(':id', 1)->queryRow();
$post2 = $command->bindValue(':id', 2)->queryRow();
195 196
```

197 198
Because [[yii\db\Command::bindParam()|bindParam()]] supports binding parameters by references, the above code
can also be written like the following:
199 200

```php
201 202 203 204 205 206 207 208
$command = $db->createCommand('SELECT * FROM post WHERE id=:id')
              ->bindParam(':id', $id);

$id = 1;
$post1 = $command->queryRow();

$id = 2;
$post2 = $command->queryRow();
209 210
```

211 212 213 214
Notice that you bind the placeholder to the `$id` 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 a new query for every different parameter value. 

215

216 217 218 219
### Executing Non-SELECT Queries <a name="non-select-queries"></a>

The `queryXyz()` methods introduced in the previous sections all deal with SELECT queries which fetch data from databases.
For queries that do not bring back data, you should call the [[yii\db\Command::execute()]] method instead. For example,
220 221

```php
222 223
$db->createCommand('UPDATE post SET status=1 WHERE id=1')
   ->execute();
224 225
```

226
The [[yii\db\Command::execute()]] method returns the number of rows affected by the SQL execution.
227

228 229 230
For INSERT, UPDATE and DELETE queries, instead of writing plain SQLs, you may call [[yii\db\Command::insert()|insert()]],
[[yii\db\Command::update()|update()]], [[yii\db\Command::delete()|delete()]], respectively, to build the corresponding
SQLs. These methods will properly quote table and column names and bind parameter values. For example,
231 232

```php
233 234
// INSERT (table name, column values)
$db->createCommand()->insert('user', [
235 236
    'name' => 'Sam',
    'age' => 30,
Alexander Makarov committed
237
])->execute();
238

239 240 241 242 243 244 245 246 247 248 249 250 251
// UPDATE (table name, column values, condition)
$db->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();

// DELETE (table name, condition)
$db->createCommand()->delete('user', 'status = 0')->execute();
```

You may also call [[yii\db\Command::batchInsert()|batchInsert()]] to insert multiple rows in one shot, which is much
more efficient than inserting one row at a time:

```php
// table name, column names, column values
$db->createCommand()->batchInsert('user', ['name', 'age'], [
252 253 254
    ['Tom', 30],
    ['Jane', 20],
    ['Linda', 25],
Alexander Makarov committed
255
])->execute();
256
```
257 258


259 260 261 262 263
## Quoting Table and Column Names <a name="quoting-table-and-column-names"></a>

When writing database-agnostic code, properly quote table and column names is often a headache because
different databases have different name quoting rules. To overcome this problem, you may use the following
quoting syntax introduced by Yii:
264

265 266
* `[[column name]]`: enclose a column name to be quoted in double square brackets; 
* `{{table name}}`: enclose a table name to be quoted in double curly brackets.
267

268 269
Yii DAO will automatically turn such constructs in a SQL into the corresponding quoted column or table names.
For example,
270 271

```php
272 273 274
// executes this SQL for MySQL: SELECT COUNT(`id`) FROM `employee`
$count = $db->createCommand("SELECT COUNT([[id]]) FROM {{employee}}")
            ->queryScalar();
275 276 277
```


278
### Using Table Prefix <a name="using-table-prefix"></a>
279

280 281
If most of your DB tables use some common prefix in their tables, you may use the table prefix feature supported
by Yii DAO.
282

283
First, specify the table prefix via the [[yii\db\Connection::tablePrefix]] property:
284 285 286 287 288 289 290 291 292 293 294 295 296 297

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

298 299 300
Then in your code, whenever you need to refer to a table whose name contains such a prefix, use the syntax
`{{%table name}}`. The percentage character will be automatically replaced with the table prefix that you have specified
when configuring the DB connection. For example,
301 302

```php
303 304 305
// executes this SQL for MySQL: SELECT COUNT(`id`) FROM `tbl_employee`
$count = $db->createCommand("SELECT COUNT([[id]]) FROM {{employee}}")
            ->queryScalar();
306
```
307 308


309
## Performing Transactions <a name="performing-transactions"></a>
310

311 312 313 314 315
When running multiple related queries in a sequence, you may need to wrap them in a transaction to ensure the integrity
and consistency of your database. If any of the queries fails, the database will be rolled back to the state as if
none of these queries is executed.
 
The following code shows a typical way of using transactions:
316 317

```php
318 319 320 321 322
$db->transaction(function($db) {
    $db->createCommand($sql1)->execute();
    $db->createCommand($sql2)->execute();
    // ... executing other SQL statements ...
});
323 324
```

325
The above code is equivalent to the following:
326 327

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

330
try {
331 332
    $db->createCommand($sql1)->execute();
    $db->createCommand($sql2)->execute();
333
    // ... executing other SQL statements ...
334
    
335
    $transaction->commit();
336
    
337
} catch(\Exception $e) {
338

339
    $transaction->rollBack();
340
    
341
    throw $e;
342 343 344
}
```

345 346 347 348 349 350
By calling the [[yii\db\Connection::beginTransaction()|beginTransaction()]] method, a new transaction is started.
The transaction is represented as a [[yii\db\Transaction]] object stored in the `$transaction` variable. Then,
the queries being executed are enclosed in a `try...catch...` block. If all queries are executed successfully,
the [[yii\db\Transaction::commit()|commit()]] method is called to commit the transaction. Otherwise, an exception
will be triggered and caught, and the [[yii\db\Transaction::rollBack()|rollBack()]] method is called to roll back
the changes made the queries prior to that failed query in the transaction.
351

352

353
### Specifying Isolation Levels <a name="specifying-isolation-levels"></a>
354

355 356
Yii also supports setting [isolation levels] for your transactions. By default, when starting a new transaction,
it will use the isolation level set by your database system. You can override the default isolation level as follows,
357

358 359
```php
$isolationLevel = \yii\db\Transaction::REPEATABLE_READ;
360

361 362 363 364 365
$db->transaction(function ($db) {
    ....
}, $isolationLevel);
 
// or alternatively
366

367
$transaction = $db->beginTransaction($isolationLevel);
368 369 370 371
```

Yii provides four constants for the most common isolation levels:

372 373 374
- [[\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.
375 376
- [[\yii\db\Transaction::SERIALIZABLE]] - the strongest level, avoids all of the above named problems.

377 378
Besides using the above constants to specify isolation levels, you may also use strings with a valid syntax supported
by the DBMS that you are using. For example, in PostgreSQL, you may use `SERIALIZABLE READ ONLY DEFERRABLE`. 
379

380 381
Note that some DBMS allow setting the isolation level only for the whole connection. Any subsequent transactions
will get the same isolation level even if you do not specify any. When using this feature
382
you may need to set the isolation level for all transactions explicitly to avoid conflicting settings.
383
At the time of this writing, only MSSQL and SQLite are affected.
384

385
> Note: SQLite only supports two isolation levels, so you can only use `READ UNCOMMITTED` and `SERIALIZABLE`.
Christopher Vrooman committed
386
Usage of other levels will result in an exception being thrown.
387

388
> Note: PostgreSQL does not allow setting the isolation level before the transaction starts so you can not
389 390
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.
391 392 393

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

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
### Nesting Transactions <a name="nesting-transactions"></a>

If your DBMS supports Savepoint, you may nest multiple transactions like the following:

```php
$db->transaction(function ($db) {
    // outer transaction
    
    $db->transaction(function ($db) {
        // inner transaction
    });
});
```

Or alternatively,

```php
$outerTransaction = $db->beginTransaction();
try {
    $db->createCommand($sql1)->execute();

    $innerTransaction = $db->beginTransaction();
    try {
        $db->createCommand($sql2)->execute();
        $innerTransaction->commit();
    } catch (Exception $e) {
        $innerTransaction->rollBack();
    }

    $outerTransaction->commit();
} catch (Exception $e) {
    $outerTransaction->rollBack();
}
```


## Replication and Read-Write Splitting <a name="read-write-splitting"></a>
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

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
Christopher Vrooman committed
486
  all other queries done through one of the "query" methods of [[yii\db\Command]] are read queries.
487 488
  You can get the currently active slave connection via `$db->slave`.

Christopher Vrooman committed
489
The `Connection` component supports load balancing and failover between slaves.
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
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
Christopher Vrooman committed
545 546
load balancing and failover between masters just as it does between slaves. A difference is that when none 
of the masters are available an exception will be thrown.
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

> 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
587 588
You may also directly set `$db->enableSlaves` to be false to direct all queries to the master connection.

589

590
## Working with Database Schema <a name="database-schema"></a>
591

592 593
Yii DAO provides a whole set of methods to let you manipulate database schema, such as creating new tables,
dropping a column from a table, etc. These methods are listed as follows:
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608
* [[yii\db\Command::createTable()|createTable()]]: creating a table
* [[yii\db\Command::renameTable()|renameTable()]]: renaming a table
* [[yii\db\Command::dropTable()|dropTable()]]: removing a table
* [[yii\db\Command::truncateTable()|truncateTable()]]: removing all rows in a table
* [[yii\db\Command::addColumn()|addColumn()]]: adding a column
* [[yii\db\Command::renameColumn()|renameColumn()]]: renaming a column
* [[yii\db\Command::dropColumn()|dropColumn()]]: removing a column
* [[yii\db\Command::alterColumn()|alterColumn()]]: altering a column
* [[yii\db\Command::addPrimaryKey()|addPrimaryKey()]]: adding a primary key
* [[yii\db\Command::dropPrimaryKey()|dropPrimaryKey()]]: removing a primary key
* [[yii\db\Command::addForeignKey()|addForeignKey()]]: adding a foreign key
* [[yii\db\Command::dropForeignKey()|dropForeignKey()]]: removing a foreign key
* [[yii\db\Command::createIndex()|createIndex()]]: creating an index
* [[yii\db\Command::dropIndex()|dropIndex()]]: removing an index
609

610
These methods can be used like the following:
611 612

```php
613
// CREATE TABLE
614
$db->createCommand()->createTable('post', [
615 616 617
    'id' => 'pk',
    'title' => 'string',
    'text' => 'text',
Alexander Makarov committed
618
]);
619 620
```

621 622 623 624 625 626 627 628 629 630
You can also retrieve the definition information about a table through 
the [[yii\db\Connection::getTableSchema()|getTableSchema()]] method of a DB connection. For example,

```php
$table = $db->getTableSchema('post');
```

The method returns a [[yii\db\TableSchema]] object which contains the information about the table's columns,
primary keys, foreign keys, etc. All these information are mainly utilized by [query builder](db-query-builder.md) 
and [active record](db-active-record.md) to help you write database-agnostic code.