## 读取器
前面读取用户生日的时候,使用了`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`属性的写入和读取的自动处理。
- 脕茫隆垄脨貌脩脭
- 脕茫隆垄脨貌脩脭
- 脪禄隆垄禄霉麓隆
- 脪禄隆垄禄霉麓隆
- 露镁隆垄URL潞脥脗路脫脡
- 露镁隆垄URL潞脥脗路脫脡
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脣脛隆垄脢媒戮脻驴芒
- 脣脛隆垄脢媒戮脻驴芒
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脕霉隆垄脛拢脨脥潞脥鹿脴脕陋
- 拢篓1拢漏脛拢脨脥露篓脪氓
- 拢篓2拢漏禄霉麓隆虏脵脳梅
- 拢篓3拢漏露脕脠隆脝梅潞脥脨脼赂脛脝梅
- 拢篓4拢漏脌脿脨脥脳陋禄禄潞脥脳脭露炉脥锚鲁脡
- 拢篓5拢漏虏茅脩炉路露脦搂
- 拢篓6拢漏脢盲脠毛潞脥脩茅脰陇
- 拢篓7拢漏鹿脴脕陋
- 拢篓8拢漏脛拢脨脥脢盲鲁枚
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 戮脜隆垄API驴陋路垄
- 戮脜隆垄API驴陋路垄
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐露镁隆垄脭脫脧卯
- Cookie
- Session
- 碌楼脭陋虏芒脢脭
- 脥录脧帽麓娄脌铆
- 脦脛录镁脡脧麓芦
- 脩茅脰陇脗毛
- 赂陆脗录
- A隆垄鲁拢录没脦脢脤芒录炉
- B隆垄3.2潞脥5.0脟酶卤冒
- C隆垄脰煤脢脰潞炉脢媒
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝