合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 类型转换 对于前面的时间戳`birthday`的例子,还可以进行进一步的简化,这里需要用到类型强制转换的功能,在`User`模型类中添加定义: ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-keyword">protected</span> $dateFormat = <span class="hljs-operator">'Y</span>/m/d'; <span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-comment">// 设置birthday为时间戳类型(整型)</span><span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestam</span>p', ]; } ``` ``` 不需要定义任何修改器和读取器,我们完成了相同的功能。 对于`timestamp`和`datetime`类型,如果不设置模型的`dateFormat`属性,默认的日期显示格式为:`Y-m-d H:i:s`,或者也可以显示的设置日期格式,例如: ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-comment">// 设置birthday为时间戳类型(整型)</span><span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestamp</span>:<span class="hljs-operator">Y</span>/m/d', ]; } ``` ``` > ### 提示: > > - - - - - - > > 对于简单的数据格式转换之类的处理,设置类型转换比定义修改器和读取器更加方便。 `ThinkPHP5.0`支持的转换类型包括: 类型 描述 integer 整型 float 浮点型 boolean 布尔型 array 数组 json JSON类型 object 对象 datetime 日期时间 timestamp 时间戳(整型) serialize 序列化## 自动时间戳 对于固定的时间戳和时间日期型的字段,比如文章的创建时间、修改时间等字段,还有比设置类型转换更简单的方法,尤其是所有的数据表统一处理的话,只需要在数据库配置文件中添加设置: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 开启自动写入时间戳字段</span><span class="hljs-string">'auto_timestamp'</span> => <span class="hljs-keyword">true</span>, ``` ``` 再次访问 ``` <pre class="calibre18"> ``` http://tp5.com/<span class="hljs-operator">user</span>/<span class="hljs-operator">add</span> ``` ``` 会发现系统已经自动写入了`think_user`数据表中的的`create_time`、`update_time`字段,如果自动写入的时间戳字段不是这两个的话,需要修改模型类的属性定义,例如: ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 定义类型转换</span><span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestamp</span>:<span class="hljs-operator">Y</span>/m/d', ]; <span class="hljs-comment">// 定义时间戳字段名</span><span class="hljs-keyword">protected</span> $createTime = <span class="hljs-operator">'create_a</span>t'; <span class="hljs-keyword">protected</span> $updateTime = <span class="hljs-operator">'update_a</span>t'; } ``` ``` 如果个别数据表不需要自动写入时间戳字段的话,也可以在模型里面直接关闭: ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 定义类型转换</span><span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestamp</span>:<span class="hljs-operator">Y</span>/m/d', ]; <span class="hljs-comment">// 关闭自动写入时间戳</span><span class="hljs-keyword">protected</span> $autoWriteTimestamp = <span class="hljs-number">false</span>; } ``` ``` 关闭自动写入时间戳后,我们再次访问URL地址: ``` <pre class="calibre18"> ``` http://tp5.com/<span class="hljs-operator">user</span>/<span class="hljs-operator">add</span> ``` ``` 重新生成的数据已经没有自动写入时间戳了,而是数据库默认值写入。 默认的时间戳字段类型是整型,如果需要使用其它的时间字段类型,可以做如下设置: ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 定义类型转换</span><span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestamp</span>:<span class="hljs-operator">Y</span>/m/d', ]; <span class="hljs-comment">// 指定自动写入时间戳的类型为dateTime类型</span><span class="hljs-keyword">protected</span> $autoWriteTimestamp = <span class="hljs-operator">'datetim</span>e'; } ``` ``` 如果全局的自动时间戳的类型是统一的,也可以直接在数据库配置文件中设置: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 开启自动写入时间戳字段</span><span class="hljs-string">'auto_timestamp'</span> => <span class="hljs-string">'datetime'</span>, ``` ``` > 如上设置后,你的`think_user`数据表中的`create_time`和 `update_time` 字段类型就必须更改为`datetime`类型的格式。 支持设置的时间戳类型包含:`datetime`、`date`和`timestamp`。 ## 自动完成 系统已经自动写入了`think_user`数据表中的的`create_time`、`update_time`字段,如果我们希望自动写入其它的字段,则可以使用自动完成功能,例如下面实现新增的时候自动写入`status`字段。 ``` <pre class="calibre18"> ``` <?php namespace app\index\model; use think\<span class="hljs-operator">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 定义类型转换</span><span class="hljs-keyword">protected</span> $<span class="hljs-operator"><span class="hljs-keyword">type</span> =</span> [ <span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'timestamp</span>:<span class="hljs-operator">Y</span>/m/d', ]; <span class="hljs-comment">// 定义自动完成的属性</span><span class="hljs-keyword">protected</span> $insert = [<span class="hljs-operator">'statu</span>s' => <span class="hljs-number">1</span>]; } ``` ``` 除了`insert`属性之外,自动完成共有三个属性定义,分别是: 属性 描述 auto 新增及更新的时候自动完成的属性数组 insert 仅新增的时候自动完成的属性数组 update 仅更新的时候自动完成的属性数组自动完成属性里面一般来说仅仅需要定义属性的名称,然后配合修改器或者类型转换来一起完成,如果写入的是一个固定的值,就无需使用修改器。`status`属性的自动写入可以直接使用: ``` <pre class="calibre18"> ``` <span class="hljs-string">'status'</span> => <span class="hljs-number">1</span> ``` ``` 完成后,我们访问URL地址: ``` <pre class="calibre18"> ``` http://tp5.com/<span class="hljs-operator">user</span>/<span class="hljs-operator">add</span> ``` ``` 最后的输出结果为: ``` <pre class="calibre18"> ``` 用户<span class="hljs-operator">[ 流年:12 ]</span>新增成功 ``` ``` 为了便于看到效果,我们修改控制器的`read`操作方法输出更多的属性: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 读取用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span>=<span class="hljs-string">''</span>)</span></span>{ <span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>); <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->email . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->birthday . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->status . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->create_time . <span class="hljs-string">'<br/>'</span>; <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->update_time . <span class="hljs-string">'<br/>'</span>; } ``` ``` 然后,访问URL地址: ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/12</span> ``` ``` 最后的输出结果为: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">1977</span>/<span class="hljs-number">03</span>/<span class="hljs-number">05</span><span class="hljs-number">1</span><span class="hljs-number">2016</span>-<span class="hljs-number">05</span>-<span class="hljs-number">02</span> <span class="hljs-number">16</span>:<span class="hljs-number">21</span>:<span class="hljs-number">33</span><span class="hljs-number">2016</span>-<span class="hljs-number">05</span>-<span class="hljs-number">02</span> <span class="hljs-number">16</span>:<span class="hljs-number">21</span>:<span class="hljs-number">33</span> ``` ``` 可以看到`status`、`create_time`和`update_time`都实现了自动写入。 如果你的`status`属性的值不是固定的,而是需要条件判断,那么我们可以定义修改器来配合自动完成。 ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Model</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Model</span></span>{ <span class="hljs-comment">// 定义类型转换</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$type</span> = [ <span class="hljs-string">'birthday'</span> => <span class="hljs-string">'timestamp:Y/m/d'</span>, ]; <span class="hljs-comment">// 定义自动完成的属性</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$insert</span> = [<span class="hljs-string">'status'</span>]; <span class="hljs-comment">// status属性修改器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setStatusAttr</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>, <span class="hljs-regexp">$data</span>)</span></span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'流年'</span> == <span class="hljs-regexp">$data</span>[<span class="hljs-string">'nickname'</span>] ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>; } <span class="hljs-comment">// status属性读取器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStatusAttr</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>)</span></span>{ <span class="hljs-regexp">$status</span> = [-<span class="hljs-number">1</span> => <span class="hljs-string">'删除'</span>, <span class="hljs-number">0</span> => <span class="hljs-string">'禁用'</span>, <span class="hljs-number">1</span> => <span class="hljs-string">'正常'</span>, <span class="hljs-number">2</span> => <span class="hljs-string">'待审核'</span>]; <span class="hljs-keyword">return</span> <span class="hljs-regexp">$status</span>[<span class="hljs-regexp">$value</span>]; } }</span> ``` ``` 我们访问下面的URL地址进行批量新增 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/add_list</span> ``` ``` 之后,访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/18</span> ``` ``` 最后的输出结果为: ``` <pre class="calibre42"> ``` 张三 zhanghsan@qq.com <span class="hljs-number">1988</span>/<span class="hljs-number">01</span>/<span class="hljs-number">15</span> 待审核 <span class="hljs-number">2016</span>-<span class="hljs-number">05</span>-<span class="hljs-number">02</span> <span class="hljs-number">16</span>:<span class="hljs-number">40</span>:<span class="hljs-number">57</span><span class="hljs-number">2016</span>-<span class="hljs-number">05</span>-<span class="hljs-number">02</span> <span class="hljs-number">16</span>:<span class="hljs-number">40</span>:<span class="hljs-number">57</span> ``` ```