-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.txt.dump
333 lines (203 loc) · 8.32 KB
/
note.txt.dump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
refresh() 或 redirect() 去避免表单重复提交
========================================================================
Yii::$app 代表应用实例, 能提供 request,response,db 等等特定功能的组件
========================================================================
Yii::$app->response->format=Response::FORMAT_JSON;//设置返回信息头json
return [];
========================================================================
return \yii\helpers\Json::encode($test);//直接返回json
========================================================================
控制器带actionHelloWorld,单词间用-间隔
========================================================================
用户浏览器跳转到 http://example.com
return $this->redirect('http://example.com');
========================================================================
默认操作设置
public $defaultAction = 'home';
========================================================================
块赋值 用户输入的表单数据赋值到 ContactForm 模型的属性
$model = new \app\models\ContactForm;
$model->attributes = \Yii::$app->request->post('ContactForm');
========================================================================
将模型转换为数组最简单的方式是使用 yii\base\Model::attributes() 属性
$post = \app\models\Post::findOne(100);
$array = $post->attributes;
更灵活和强大的将模型转换为数组的方式是使用 yii\base\Model::toArray() 方法
========================================================================
render(): 渲染一个 视图名 并使用一个 布局 返回到渲染结果。
renderPartial(): 渲染一个 视图名 并且不使用布局。
renderAjax(): 渲染一个 视图名 并且不使用布局, 并注入所有注册的JS/CSS脚本和文件,通常使用在响应AJAX网页请求的情况下。
查询
Test::findBySql($sql)->all();//支持绑定参数
Test::find()->where(array)->all();
删除
Test::deleteAll('id>:id',array(':id'=>0));
添加
$test = new Test;
$test->id = 3;
$test->title = 'name';
$test->validate();
if($test->hasError()){
die;
}
$test->save();
数据验证在模型中的rules方法
修改
$test = Test::find()->where(['id'=>4])->one();
$test->title = '111';
$test->save();
关联查询
$customer = Customer::find()->where(['name'=>'zhangshan'])->one();
======================================================================
可以写在models里
$order = $customer->hasMany(Order::calssName(),['customer_id'=>'id'])->asArray()->all();
$orders = $customer->orders;//自动调用Customer::getOrders方法,顺便加上调用all();
关联查询结果会缓存,使用unset($customer->order)释放
$customer->find()->with('orders')->all();//优化foreach多次sql执行
=======================================================================
Query Builder
$rows = (new \yii\db\Query())
->select(['dyn_id', 'dyn_name'])
->from('zs_dynasty')
->where(['between','dyn_id', 1,30])
->limit(10)
->all();
print_r($rows);
=======================================================================
use yii\db\Query;
$query = (new Query())
->from('user')
->orderBy('id');
=======================================================================
SELECT
$query->select('*')->
select('dyn_id as id, dynasty.dyn_name')->
$query->select(['dyn_id as id', "CONCAT(dyn_name,'a')"])->
$query->select('user_id')->distinct()->
=======================================================================
FORM
$query->from('user');
$query->from(['public.user u', 'public.post p']);
$query->from('public.user u, public.post p');
$query->from(['u' => 'public.user', 'p' => 'public.post']);
----------
$subQuery = (new Query())->select('id')->from('user')->where('status=1');
// SELECT * FROM (SELECT `id` FROM `user` WHERE status=1) u
$query->from(['u' => $subQuery]);
=======================================================================
WHERE
where('status=1')->
where('status=:status', [':status' => $status])->
where([
'status' => 10,
'type' => null,
'id' => [4, 8, 15],
])->
-------
$userQuery = (new Query())->select('id')->from('user');
// ...WHERE `id` IN (SELECT `id` FROM `user`)
$query->...->where(['id' => $userQuery])->...
--------
['and', 'id=1', 'id=2'] //id=1 AND id=2
['and', 'type=1', ['or', 'id=1', 'id=2']] //type=1 AND (id=1 OR id=2)
['between', 'id', 1, 10] //id BETWEEN 1 AND 10
['not between', 'id', 1, 10] //not id BETWEEN 1 AND 10
['in', 'id', [1, 2, 3]] //id IN (1, 2, 3)
['not in', 'id', [1, 2, 3]] //not id IN (1, 2, 3)
['like', 'name', 'tester'] //name LIKE '%tester%'
['like', 'name', ['test', 'sample']] //name LIKE '%test%' AND name LIKE '%sample%'
['not like', 'name', ['or', 'test', 'sample']] //not name LIKE '%test%' OR not name LIKE '%sample%'
['exists','id', $userQuery] //EXISTS (sub-query) | not exists
['>', 'age', 10] //age>10
=======================================================================
ADD WHERE
$status = 10;
$search = 'yii';
$query->where(['status' => $status]);
if (!empty($search)) {
$query->andWhere(['like', 'title', $search]);
}
//WHERE (`status` = 10) AND (`title` LIKE '%yii%')
//andWhere() or orWhere()
=======================================================================
FILTER WHERE
$query->filterWhere([
'username' => $username,
'email' => $email,
]);
//如果email为空,则 WHERE username=:username
=======================================================================
ORDER BY
$query->orderBy([
'id' => SORT_ASC,
'name' => SORT_DESC,
]);
//orderBy , addOrderBy
=======================================================================
GROUP BY
$query->groupBy('id, status');
$query->addGroupBy(['created_at', 'updated_at']);
=======================================================================
HAVING
$query->having(['status' => $status]);
//having,andHaving,orHaving
=======================================================================
LIMIT OR OFFSET
$query->limit(10);
$query->offset(10);
=======================================================================
JOIN
innerJoin()
leftJoin()
rightJoin()
$query->select(['user.name AS author', 'post.title as title'])
->from('user')
->leftJoin('post', 'post.user_id = user.id');
$query->join('FULL OUTER JOIN', 'post', 'post.user_id = user.id');
$query->leftJoin(['u' => $subQuery], 'u.id=author_id');
=======================================================================
UNION
$query = new Query();
$query->select("id, category_id as type, name")->from('post')->limit(10);
$anotherQuery = new Query();
$anotherQuery->select('id, type, name')->from('user')->limit(10);
$query->union($anotherQuery);
=======================================================================
QUERY METHODS
all() //所有行列
one() //第一行
column() //第一列
scalar() //第一行第一列
exists() //是否有结果存在
count() //记录数量
sum($q), average($q), max($q), min($q) //$q 为字段或表达式
$count = (new \yii\db\Query())
->from('user')
->where(['last_name' => 'Smith'])
->count();
//SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name
$command = (new \yii\db\Query())
->select(['id', 'email'])
->from('user')
->where(['last_name' => 'Smith'])
->limit(10)
->createCommand();
// show the SQL statement
echo $command->sql;
// show the parameters to be bound
print_r($command->params);
// returns all rows of the query result
$rows = $command->queryAll();
=======================================================================
打印sql语句
$query = User::find()->where(['id'=>[1,2,3,4]); // 要去掉最后->all()
echo $query->createCommand()->getRawSql();
=======================================================================
//查询
$db=\Yii::$app->db ->createCommand("select * from 表名") ->queryAll();
//修改
$db=\Yii::$app->db ->createCommand()->update('表名',['字段名'=>要修改的值],'条件') ->execute();
// 删除
$db=\Yii::$app->db ->createCommand() ->delete('表名','条件') ->execute();
//添加
$db=\Yii::$app->db ->createCommand() ->insert('表名',['字段名'=>要添加的值],'条件') ->execute();