Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
271aecb0
Commit
271aecb0
authored
Jul 03, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update database-basics.md
parent
8036d416
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
6 deletions
+8
-6
database-basics.md
docs/guide/database-basics.md
+8
-6
No files found.
docs/guide/database-basics.md
View file @
271aecb0
...
@@ -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 component
s
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
//
UPDAT
E
//
CREATE TABL
E
$connection
->
createCommand
()
->
createTable
(
'tbl_post'
,
array
(
$connection
->
createCommand
()
->
createTable
(
'tbl_post'
,
array
(
'id'
=>
'pk'
,
'id'
=>
'pk'
,
'title'
=>
'string'
,
'title'
=>
'string'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment