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
25c6547f
Commit
25c6547f
authored
Dec 04, 2014
by
Lynnworld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update db-dao.md
parent
c8be3dd2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
2 deletions
+46
-2
db-dao.md
docs/guide-zh-CN/db-dao.md
+46
-2
No files found.
docs/guide-zh-CN/db-dao.md
View file @
25c6547f
...
@@ -45,20 +45,62 @@ return [
...
@@ -45,20 +45,62 @@ return [
```
$connection = \Yii::$app->db;```
```
$connection = \Yii::$app->db;```
请参考
```[[yii\db\Connection]]```
获取可配置的属性列表。也请注意如果需要同时使用多个数据库可以定义 多个 连接组件:
请参考```[[yii\db\Connection]]```获取可配置的属性列表。
如果你想通过ODBC连接数据库,则需要配置[[yii\db\Connection::driverName]] 属性,例如:
```
'db' =>
[
'class' => 'yii
\d
b
\C
onnection',
'driverName' => 'mysql',
'dsn' => 'odbc:Driver={MySQL};Server=localhost;Database=test',
'username' => 'root',
'password' => '',
],
```
注意:如果需要同时使用多个数据库可以定义 多个 连接组件:
```
php
return
[
// ...
'components' =>
[
// ...
'db' =>
[
'class' => 'yii
\d
b
\C
onnection',
'dsn' => 'mysql:host=localhost;dbname=mydatabase',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'secondDb' =>
[
'class' => 'yii
\d
b
\C
onnection',
'dsn' => 'sqlite:/path/to/database/file',
],
],
// ...
];
```
在代码中通过以下方式使用:
```
$primaryConnection =
\Y
ii::$app->db;
$primaryConnection =
\Y
ii::$app->db;
$secondaryConnection =
\Y
ii::$app->secondDb;
$secondaryConnection =
\Y
ii::$app->secondDb;
如果不想定义数据库连接为应用组件,可以直接初始化使用:
```
如果不想定义数据库连接为全局[应用](structure-application-components.md)组件,可以在代码中直接初始化使用:
```
$connection = new
\y
ii
\d
b
\C
onnection(
[
$connection = new
\y
ii
\d
b
\C
onnection(
[
'dsn' => $dsn,
'dsn' => $dsn,
'username' => $username,
'username' => $username,
'password' => $password,
'password' => $password,
]);
]);
$connection->open();
$connection->open();
```
小提示:如果在创建了连接后需要执行额外的 SQL 查询,可以添加以下代码到应用配置文件:
小提示:如果在创建了连接后需要执行额外的 SQL 查询,可以添加以下代码到应用配置文件:
```
return
[
return
[
// ...
// ...
'components' =>
[
'components' =>
[
...
@@ -73,6 +115,8 @@ return [
...
@@ -73,6 +115,8 @@ return [
],
],
// ...
// ...
];
];
```
SQL 基础查询
SQL 基础查询
一旦有了连接实例就可以通过
[
[yii\db\Command
]
]执行 SQL 查询。
一旦有了连接实例就可以通过
[
[yii\db\Command
]
]执行 SQL 查询。
...
...
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