File tree Expand file tree Collapse file tree 12 files changed +77
-14
lines changed Expand file tree Collapse file tree 12 files changed +77
-14
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : post
3
+ title : " yii2demo分析之Model类一(公共目录下的Model)"
4
+ date : 2019-04-13 16:44:12 +0800
5
+ tags :
6
+ description :
7
+ ---
8
+
9
+ 填坑的时间有点长了。。。我是罪人,直接正题吧
10
+
11
+ yii2的demo中,目录结构如下
12
+ ![ ] ( /images/2019-04-13-1.jpg )
13
+
14
+ backend和frontend两个目录分别是站点后台和站点前台的目录,common目录为公共目录,公共的Model、配置、组件或者其他等文件的存放目录,但是在demo目录中common目录和前后台目录中都存在Model目录,本次介绍主要为common目录中的Model类的作用
15
+ ![ ] ( /images/2019-04-13-2.jpg )
16
+
17
+ 以common/models目录中的User.php为例,内容如下(依然是坑爹的笔记本屏幕):
18
+ ![ ] ( /images/2019-04-13-3.jpg )
19
+ ![ ] ( /images/2019-04-13-4.jpg )
20
+
21
+ 该类型文件可用gii自动生成,注释部分为对应的数据表字段(PS: User .php中实现了IdentityInterface接口,该接口作用在[ 2018-12-08-yii2demo分析之后台访问权限验证流程分析] ( https://guosongyu.github.io/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 ) 进行过介绍,大部分Model类不不需要实现此接口)
22
+
23
+ 该文件继承ActiveRecord类,在yii中,该类用以访问和操作数据库中的数据,与数据库进行关联。
24
+ 本例中,tableName和rules两个函数可以自动生成,前者返回关联的数据库名称,后者在保存字段时对字段进行验证,behaviors函数中可以根据需求实现一些行为逻辑,对数据进行加工,典型的MVC模式中的M层结构。
25
+
26
+ 该类型的Model文件中函数建议大部分为直接操作数据的函数,如查询、保存等;或加工字段,如把要保存的字段格式处理为数据库中的格式。
27
+
28
+ 该类中能够处理绝大多数的数据库操作,如关联查询、事务、自定义查询字段等,能够大大减少时间成本,对于该类中的详细用法,我就不再一次进行重复了。 [ 文档] ( https://www.yiichina.com/doc/guide/2.0/db-active-record ) 中的介绍远比我详细的多。。。
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "github”</h2>
54
54
<!-- <ul> -->
55
55
56
56
57
+
58
+
57
59
58
60
59
61
@@ -224,6 +226,8 @@ <h2 class="post-title">
224
226
< h2 > 近期文章</ h2 >
225
227
< ul >
226
228
229
+ < li > < a href ="/2019/04/yii2demo%E5%88%86%E6%9E%90%E4%B9%8BModel%E7%B1%BB%E4%B8%80-%E5%85%AC%E5%85%B1%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84Model "> yii2demo分析之Model类一(公共目录下的Model)</ a > </ li >
230
+
227
231
< li > < a href ="/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 "> yii2demo分析之验证码</ a > </ li >
228
232
229
233
< li > < a href ="/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 "> yii2demo分析之后台访问权限验证流程分析</ a > </ li >
@@ -232,8 +236,6 @@ <h2>近期文章</h2>
232
236
233
237
< li > < a href ="/2018/06/%E6%9C%80%E8%BF%91%E6%9C%89%E4%BA%9B%E8%BF%B7%E8%8C%AB "> 最近有些迷茫</ a > </ li >
234
238
235
- < li > < a href ="/2017/10/mysql%E4%B8%ADdate%E5%AD%97%E6%AE%B5%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4'0000-00-00'%E6%8A%A5%E9%94%99%E7%9A%84%E9%97%AE%E9%A2%98 "> mysql中date字段设置默认'0000-00-00'报错的问题</ a > </ li >
236
-
237
239
</ ul >
238
240
</ div >
239
241
@@ -285,6 +287,9 @@ <h2>文章分类</h2>
285
287
< script type ="text/javascript "> var cnzz_protocol = ( ( "https:" == document . location . protocol ) ? " https://" : " http://" ) ; document . write ( unescape ( "%3Cspan id='cnzz_stat_icon_1261163191'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1261163191%26show%3Dpic2' type='text/javascript'%3E%3C/script%3E" ) ) ; </ script >
286
288
287
289
290
+
291
+ < a target ="_blank " href ="https://996.icu "> < img src ="https://img.shields.io/badge/link-996.icu-red.svg " alt ="996.icu " /> </ a >
292
+
288
293
</ p >
289
294
< h6 > Follow me</ h6 >
290
295
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "jekyll”</h2>
54
54
<!-- <ul> -->
55
55
56
56
57
+
58
+
57
59
58
60
59
61
@@ -224,6 +226,8 @@ <h2 class="post-title">
224
226
< h2 > 近期文章</ h2 >
225
227
< ul >
226
228
229
+ < li > < a href ="/2019/04/yii2demo%E5%88%86%E6%9E%90%E4%B9%8BModel%E7%B1%BB%E4%B8%80-%E5%85%AC%E5%85%B1%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84Model "> yii2demo分析之Model类一(公共目录下的Model)</ a > </ li >
230
+
227
231
< li > < a href ="/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 "> yii2demo分析之验证码</ a > </ li >
228
232
229
233
< li > < a href ="/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 "> yii2demo分析之后台访问权限验证流程分析</ a > </ li >
@@ -232,8 +236,6 @@ <h2>近期文章</h2>
232
236
233
237
< li > < a href ="/2018/06/%E6%9C%80%E8%BF%91%E6%9C%89%E4%BA%9B%E8%BF%B7%E8%8C%AB "> 最近有些迷茫</ a > </ li >
234
238
235
- < li > < a href ="/2017/10/mysql%E4%B8%ADdate%E5%AD%97%E6%AE%B5%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4'0000-00-00'%E6%8A%A5%E9%94%99%E7%9A%84%E9%97%AE%E9%A2%98 "> mysql中date字段设置默认'0000-00-00'报错的问题</ a > </ li >
236
-
237
239
</ ul >
238
240
</ div >
239
241
@@ -285,6 +287,9 @@ <h2>文章分类</h2>
285
287
< script type ="text/javascript "> var cnzz_protocol = ( ( "https:" == document . location . protocol ) ? " https://" : " http://" ) ; document . write ( unescape ( "%3Cspan id='cnzz_stat_icon_1261163191'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1261163191%26show%3Dpic2' type='text/javascript'%3E%3C/script%3E" ) ) ; </ script >
286
288
287
289
290
+
291
+ < a target ="_blank " href ="https://996.icu "> < img src ="https://img.shields.io/badge/link-996.icu-red.svg " alt ="996.icu " /> </ a >
292
+
288
293
</ p >
289
294
< h6 > Follow me</ h6 >
290
295
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "php”</h2>
54
54
<!-- <ul> -->
55
55
56
56
57
+
58
+
57
59
58
60
59
61
@@ -242,6 +244,8 @@ <h2 class="post-title">
242
244
< h2 > 近期文章</ h2 >
243
245
< ul >
244
246
247
+ < li > < a href ="/2019/04/yii2demo%E5%88%86%E6%9E%90%E4%B9%8BModel%E7%B1%BB%E4%B8%80-%E5%85%AC%E5%85%B1%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84Model "> yii2demo分析之Model类一(公共目录下的Model)</ a > </ li >
248
+
245
249
< li > < a href ="/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 "> yii2demo分析之验证码</ a > </ li >
246
250
247
251
< li > < a href ="/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 "> yii2demo分析之后台访问权限验证流程分析</ a > </ li >
@@ -250,8 +254,6 @@ <h2>近期文章</h2>
250
254
251
255
< li > < a href ="/2018/06/%E6%9C%80%E8%BF%91%E6%9C%89%E4%BA%9B%E8%BF%B7%E8%8C%AB "> 最近有些迷茫</ a > </ li >
252
256
253
- < li > < a href ="/2017/10/mysql%E4%B8%ADdate%E5%AD%97%E6%AE%B5%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4'0000-00-00'%E6%8A%A5%E9%94%99%E7%9A%84%E9%97%AE%E9%A2%98 "> mysql中date字段设置默认'0000-00-00'报错的问题</ a > </ li >
254
-
255
257
</ ul >
256
258
</ div >
257
259
@@ -303,6 +305,9 @@ <h2>文章分类</h2>
303
305
< script type ="text/javascript "> var cnzz_protocol = ( ( "https:" == document . location . protocol ) ? " https://" : " http://" ) ; document . write ( unescape ( "%3Cspan id='cnzz_stat_icon_1261163191'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1261163191%26show%3Dpic2' type='text/javascript'%3E%3C/script%3E" ) ) ; </ script >
304
306
305
307
308
+
309
+ < a target ="_blank " href ="https://996.icu "> < img src ="https://img.shields.io/badge/link-996.icu-red.svg " alt ="996.icu " /> </ a >
310
+
306
311
</ p >
307
312
< h6 > Follow me</ h6 >
308
313
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "web”</h2>
54
54
<!-- <ul> -->
55
55
56
56
57
+
58
+
57
59
58
60
59
61
@@ -188,6 +190,8 @@ <h2 class="post-title">
188
190
< h2 > 近期文章</ h2 >
189
191
< ul >
190
192
193
+ < li > < a href ="/2019/04/yii2demo%E5%88%86%E6%9E%90%E4%B9%8BModel%E7%B1%BB%E4%B8%80-%E5%85%AC%E5%85%B1%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84Model "> yii2demo分析之Model类一(公共目录下的Model)</ a > </ li >
194
+
191
195
< li > < a href ="/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 "> yii2demo分析之验证码</ a > </ li >
192
196
193
197
< li > < a href ="/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 "> yii2demo分析之后台访问权限验证流程分析</ a > </ li >
@@ -196,8 +200,6 @@ <h2>近期文章</h2>
196
200
197
201
< li > < a href ="/2018/06/%E6%9C%80%E8%BF%91%E6%9C%89%E4%BA%9B%E8%BF%B7%E8%8C%AB "> 最近有些迷茫</ a > </ li >
198
202
199
- < li > < a href ="/2017/10/mysql%E4%B8%ADdate%E5%AD%97%E6%AE%B5%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4'0000-00-00'%E6%8A%A5%E9%94%99%E7%9A%84%E9%97%AE%E9%A2%98 "> mysql中date字段设置默认'0000-00-00'报错的问题</ a > </ li >
200
-
201
203
</ ul >
202
204
</ div >
203
205
@@ -249,6 +251,9 @@ <h2>文章分类</h2>
249
251
< script type ="text/javascript "> var cnzz_protocol = ( ( "https:" == document . location . protocol ) ? " https://" : " http://" ) ; document . write ( unescape ( "%3Cspan id='cnzz_stat_icon_1261163191'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1261163191%26show%3Dpic2' type='text/javascript'%3E%3C/script%3E" ) ) ; </ script >
250
252
251
253
254
+
255
+ < a target ="_blank " href ="https://996.icu "> < img src ="https://img.shields.io/badge/link-996.icu-red.svg " alt ="996.icu " /> </ a >
256
+
252
257
</ p >
253
258
< h6 > Follow me</ h6 >
254
259
Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "yii2”</h2>
54
54
<!-- <ul> -->
55
55
56
56
57
+
58
+
57
59
58
60
< h2 class ="post-title ">
59
61
< a href ="https://GuoSongyu.github.io/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 ">
@@ -188,6 +190,8 @@ <h2 class="post-title">
188
190
< h2 > 近期文章</ h2 >
189
191
< ul >
190
192
193
+ < li > < a href ="/2019/04/yii2demo%E5%88%86%E6%9E%90%E4%B9%8BModel%E7%B1%BB%E4%B8%80-%E5%85%AC%E5%85%B1%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84Model "> yii2demo分析之Model类一(公共目录下的Model)</ a > </ li >
194
+
191
195
< li > < a href ="/2019/01/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E9%AA%8C%E8%AF%81%E7%A0%81 "> yii2demo分析之验证码</ a > </ li >
192
196
193
197
< li > < a href ="/2018/12/yii2demo%E5%88%86%E6%9E%90%E4%B9%8B%E5%90%8E%E5%8F%B0%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90 "> yii2demo分析之后台访问权限验证流程分析</ a > </ li >
@@ -196,8 +200,6 @@ <h2>近期文章</h2>
196
200
197
201
< li > < a href ="/2018/06/%E6%9C%80%E8%BF%91%E6%9C%89%E4%BA%9B%E8%BF%B7%E8%8C%AB "> 最近有些迷茫</ a > </ li >
198
202
199
- < li > < a href ="/2017/10/mysql%E4%B8%ADdate%E5%AD%97%E6%AE%B5%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4'0000-00-00'%E6%8A%A5%E9%94%99%E7%9A%84%E9%97%AE%E9%A2%98 "> mysql中date字段设置默认'0000-00-00'报错的问题</ a > </ li >
200
-
201
203
</ ul >
202
204
</ div >
203
205
@@ -249,6 +251,9 @@ <h2>文章分类</h2>
249
251
< script type ="text/javascript "> var cnzz_protocol = ( ( "https:" == document . location . protocol ) ? " https://" : " http://" ) ; document . write ( unescape ( "%3Cspan id='cnzz_stat_icon_1261163191'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1261163191%26show%3Dpic2' type='text/javascript'%3E%3C/script%3E" ) ) ; </ script >
250
252
251
253
254
+
255
+ < a target ="_blank " href ="https://996.icu "> < img src ="https://img.shields.io/badge/link-996.icu-red.svg " alt ="996.icu " /> </ a >
256
+
252
257
</ p >
253
258
< h6 > Follow me</ h6 >
254
259
You can’t perform that action at this time.
0 commit comments