合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 读取器 前面读取用户生日的时候,使用了`date`方法进行日期的格式处理输出,但是每次读取数据后都需要这样处理就显得非常麻烦。 使用读取器功能就可以简化类似的数据处理操作,例如,我们给`User`模型添加读取器的定义方法。 ``` <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">// birthday读取器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getBirthdayAttr</span><span class="hljs-number">(<span class="hljs-regexp">$birthday</span>)</span></span>{ <span class="hljs-keyword">return</span> date(<span class="hljs-string">'Y-m-d'</span>, <span class="hljs-regexp">$birthday</span>); } }</span> ``` ``` 这里,我们添加了一个`getBirthdayAttr`读取器方法用于读取`User`模型的`birthday`属性的值,该方法会在读取birthday属性值的时候自动执行。 读取器方法的命名规范是: > ### get + 属性名的驼峰命名+ Attr 所以,`getBirthdayAttr`读取器读取的是`birthday`属性,而`getUserBirthdayAttr` 读取器读取的则是`user_birthday`属性。 定义完修改器后,修改控制器的`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>; } ``` ``` 访问URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/1</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> ``` ``` 读取器还可以定义读取数据表中不存在的属性,例如把原始生日和转换的格式分开两个属性`birthday`和`user_birthday`,我们只需定义`user_birthday`属性的读取器方法: ``` <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">// user_birthday读取器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserBirthdayAttr</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> date(<span class="hljs-string">'Y-m-d'</span>, <span class="hljs-regexp">$data</span>[<span class="hljs-string">'birthday'</span>]); } }</span> ``` ``` 这里的读取器方法使用了第二个参数,表示传入所有的属性数据。因为原始的`user_birthday`属性数据是不存在的,所以我们需要通过`data`参数获取。 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>->user_birthday . <span class="hljs-string">'<br/>'</span>; } ``` ``` 当刷新页面的时候,最终输出的结果为: ``` <pre class="calibre18"> ``` 流年 thinkphp@qq.com <span class="hljs-number">226339200</span><span class="hljs-number">1977</span>-<span class="hljs-number">03</span>-<span class="hljs-number">05</span> ``` ``` ## 修改器 由于`birthday`属性是时间戳(整型)格式的,因此我们必须在写入数据前进行时间戳转换,前面使用的方法是每次赋值的时候进行转换处理: ``` <pre class="calibre18"> ``` <span class="hljs-regexp">$user</span>[<span class="hljs-string">'birthday'</span>] = strtotime(<span class="hljs-string">'2015-04-02'</span>); ``` ``` 为了避免每次都进行日期格式的转换操作,可以定义修改器方法来自动处理,修改`User`模型如下: ``` <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-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserBirthdayAttr</span><span class="hljs-number">(<span class="hljs-regexp">$birthday</span>, <span class="hljs-regexp">$data</span>)</span></span>{ <span class="hljs-keyword">return</span> date(<span class="hljs-string">'Y-m-d'</span>, <span class="hljs-regexp">$data</span>[<span class="hljs-string">'birthday'</span>]); } <span class="hljs-comment">// birthday修改器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setBirthdayAttr</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>)</span></span>{ <span class="hljs-keyword">return</span> strtotime(<span class="hljs-regexp">$value</span>); } }</span> ``` ``` 修改器方法的命名规范是: > ### set + 属性名的驼峰命名+ Attr 所以,`setBirthdayAttr`方法修改的是`birthday`属性,而`setUserBirthdayAttr` 方法修改的则是`user_birthday`属性。 控制器的`add`操作方法修改如下: ``` <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">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-regexp">$user</span>->nickname = <span class="hljs-string">'流年'</span>; <span class="hljs-regexp">$user</span>->email = <span class="hljs-string">'thinkphp@qq.com'</span>; <span class="hljs-regexp">$user</span>->birthday = <span class="hljs-string">'1977-03-05'</span>; <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->save()) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` 访问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">[ 流年:10 ]</span>新增成功 ``` ``` 接着我们访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/10</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> ``` ``` 通过定义修改器和读取器,完成了时间戳方式存储的`birthday`属性的写入和读取的自动处理。