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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
fa86bebb
Commit
fa86bebb
authored
Dec 04, 2014
by
Lynnworld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update db-dao.md
parent
25c6547f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
7 deletions
+17
-7
db-dao.md
docs/guide-zh-CN/db-dao.md
+17
-7
No files found.
docs/guide-zh-CN/db-dao.md
View file @
fa86bebb
...
...
@@ -98,7 +98,7 @@ $connection = new \yii\db\Connection([
$connection->open();
```
小提示:如果在创建了连接后需要执行额外的 SQL 查询,可以添加以下代码到应用配置文件:
>
小提示:如果在创建了连接后需要执行额外的 SQL 查询,可以添加以下代码到应用配置文件:
```
return
[
...
...
@@ -117,34 +117,44 @@ return [
];
```
SQL 基础查询
##
SQL 基础查询
一旦有了连接实例就可以通过[[yii\db\Command]]执行 SQL 查询。
SELECT 查询
###SELECT 查询
查询返回多行:
```
$command = $connection->createCommand('SELECT
*
FROM post');
$posts = $command->queryAll();
```
返回单行:
```
$command = $connection->createCommand('SELECT
*
FROM post WHERE id=1');
$post = $command->queryOne();
查询多列值:
```
查询多行单值:
```
$command = $connection->createCommand('SELECT title FROM post');
$titles = $command->queryColumn();
```
查询标量值/计算值:
```
$command = $connection->createCommand('SELECT COUNT(
*
) FROM post');
$postCount = $command->queryScalar();
UPDATE, INSERT, DELETE 更新、插入和删除等
```
###UPDATE, INSERT, DELETE 更新、插入和删除等
如果执行 SQL 不返回任何数据可使用命令中的 execute 方法:
```
$command = $connection->createCommand('UPDATE post SET status=1 WHERE id=1');
$command->execute();
```
选择以下考虑到引用了恰当表名和列名的语法是可能的:
// INSERT
...
...
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