Commit 271aecb0 by Carsten Brandt

Update database-basics.md

parent 8036d416
...@@ -5,6 +5,7 @@ Yii has a database access layer built on top of PHP's [PDO](http://www.php.net/m ...@@ -5,6 +5,7 @@ Yii has a database access layer built on top of PHP's [PDO](http://www.php.net/m
uniform API and solves some inconsistencies between different DBMS. By default Yii supports MySQL, SQLite, PostgreSQL, uniform API and solves some inconsistencies between different DBMS. By default Yii supports MySQL, SQLite, PostgreSQL,
Oracle and MSSQL. Oracle and MSSQL.
Configuration Configuration
------------- -------------
...@@ -28,21 +29,21 @@ return array( ...@@ -28,21 +29,21 @@ return array(
); );
``` ```
After component is configured you can access using the following syntax: After the component is configured you can access it using the following syntax:
```php ```php
$connection = \Yii::$app->db; $connection = \Yii::$app->db;
``` ```
You can refer to [[\yii\db\Connection]] for a list of properties you can configure. Also note that you can define more You can refer to [[\yii\db\Connection]] for a list of properties you can configure. Also note that you can define more
than one connection components and use both at the same time if needed: than one connection component and use both at the same time if 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 connection as application component you can instantiate it directly: If you don't want to define the connection as an application component you can instantiate it directly:
```php ```php
$connection = new \yii\db\Connection(array( $connection = new \yii\db\Connection(array(
...@@ -53,6 +54,7 @@ $connection = new \yii\db\Connection(array( ...@@ -53,6 +54,7 @@ $connection = new \yii\db\Connection(array(
$connection->open(); $connection->open();
``` ```
Basic SQL queries Basic SQL queries
----------------- -----------------
...@@ -70,7 +72,7 @@ $posts = $command->queryAll(); ...@@ -70,7 +72,7 @@ $posts = $command->queryAll();
When only a single row is returned: When only a single row is returned:
```php ```php
$command = $connection->createCommand('SELECT * FROM tbl_post LIMIT 1'); $command = $connection->createCommand('SELECT * FROM tbl_post WHERE id=1');
$post = $command->query(); $post = $command->query();
``` ```
...@@ -93,7 +95,7 @@ $postCount = $command->queryScalar(); ...@@ -93,7 +95,7 @@ $postCount = $command->queryScalar();
If SQL executed doesn't return any data you can use command's `execute` method: If SQL executed doesn't return any data you can use command's `execute` method:
```php ```php
$command = $connection->createCommand('UPDATE tbl_post SET status=1'); $command = $connection->createCommand('UPDATE tbl_post SET status=1 WHERE id=1');
$command->execute(); $command->execute();
``` ```
...@@ -196,7 +198,7 @@ Aside from basic SQL queries [[\yii\db\Command]] contains a set of methods allow ...@@ -196,7 +198,7 @@ Aside from basic SQL queries [[\yii\db\Command]] contains a set of methods allow
These can be used as follows: These can be used as follows:
```php ```php
// UPDATE // CREATE TABLE
$connection->createCommand()->createTable('tbl_post', array( $connection->createCommand()->createTable('tbl_post', array(
'id' => 'pk', 'id' => 'pk',
'title' => 'string', 'title' => 'string',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment