Skip to content

Commit 7d38fa6

Browse files
author
Songyu By Home
committed
更新文章
1 parent acf08c1 commit 7d38fa6

File tree

12 files changed

+77
-14
lines changed

12 files changed

+77
-14
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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)中的介绍远比我详细的多。。。

images/2019-04-13-1.jpg

15 KB
Loading

images/2019-04-13-2.jpg

29.4 KB
Loading

images/2019-04-13-3.jpg

136 KB
Loading

images/2019-04-13-4.jpg

120 KB
Loading

tag/github/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "github&rdquo;</h2>
5454
<!-- <ul> -->
5555

5656

57+
58+
5759

5860

5961

@@ -224,6 +226,8 @@ <h2 class="post-title">
224226
<h2>近期文章</h2>
225227
<ul>
226228

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+
227231
<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>
228232

229233
<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>
232236

233237
<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>
234238

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-
237239
</ul>
238240
</div>
239241

@@ -285,6 +287,9 @@ <h2>文章分类</h2>
285287
<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>
286288

287289

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+
288293
</p>
289294
<h6>Follow me</h6>
290295

tag/jekyll/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "jekyll&rdquo;</h2>
5454
<!-- <ul> -->
5555

5656

57+
58+
5759

5860

5961

@@ -224,6 +226,8 @@ <h2 class="post-title">
224226
<h2>近期文章</h2>
225227
<ul>
226228

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+
227231
<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>
228232

229233
<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>
232236

233237
<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>
234238

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-
237239
</ul>
238240
</div>
239241

@@ -285,6 +287,9 @@ <h2>文章分类</h2>
285287
<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>
286288

287289

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+
288293
</p>
289294
<h6>Follow me</h6>
290295

tag/php/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "php&rdquo;</h2>
5454
<!-- <ul> -->
5555

5656

57+
58+
5759

5860

5961

@@ -242,6 +244,8 @@ <h2 class="post-title">
242244
<h2>近期文章</h2>
243245
<ul>
244246

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+
245249
<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>
246250

247251
<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>
250254

251255
<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>
252256

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-
255257
</ul>
256258
</div>
257259

@@ -303,6 +305,9 @@ <h2>文章分类</h2>
303305
<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>
304306

305307

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+
306311
</p>
307312
<h6>Follow me</h6>
308313

tag/web/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "web&rdquo;</h2>
5454
<!-- <ul> -->
5555

5656

57+
58+
5759

5860

5961

@@ -188,6 +190,8 @@ <h2 class="post-title">
188190
<h2>近期文章</h2>
189191
<ul>
190192

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+
191195
<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>
192196

193197
<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>
196200

197201
<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>
198202

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-
201203
</ul>
202204
</div>
203205

@@ -249,6 +251,9 @@ <h2>文章分类</h2>
249251
<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>
250252

251253

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+
252257
</p>
253258
<h6>Follow me</h6>
254259

tag/yii2/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h2 class="post_title">标签分类 "yii2&rdquo;</h2>
5454
<!-- <ul> -->
5555

5656

57+
58+
5759

5860
<h2 class="post-title">
5961
<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">
188190
<h2>近期文章</h2>
189191
<ul>
190192

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+
191195
<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>
192196

193197
<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>
196200

197201
<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>
198202

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-
201203
</ul>
202204
</div>
203205

@@ -249,6 +251,9 @@ <h2>文章分类</h2>
249251
<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>
250252

251253

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+
252257
</p>
253258
<h6>Follow me</h6>
254259

0 commit comments

Comments
 (0)