合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
以`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>{ } ``` ``` 可以输出模型实例对象为数组或者`JSON`。 - - [输出数组](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u8F93u51FAu6570u7EC4) - [隐藏属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u9690u85CFu5C5Eu6027) - [指定属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u6307u5B9Au5C5Eu6027) - [追加属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u8FFDu52A0u5C5Eu6027) - [输出JSON](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#-json) ## 输出数组 可以使用`toArray`方法把当前的模型对象输出为数组。 修改`User`控制器的`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>); dump(<span class="hljs-regexp">$user</span>->toArray()); } ``` ``` 访问 `http://tp5.com/user/20` 页面输出结果为: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">array</span> (size=<span class="hljs-number">7</span>) <span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>) <span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>) <span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>) <span class="hljs-string">'status'</span> => string <span class="hljs-string">'待审核'</span> (length=<span class="hljs-number">9</span>) <span class="hljs-string">'create_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>) <span class="hljs-string">'update_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>) ``` ``` 可以看到,User模型的数据已经经过了读取器方法处理。 ## 隐藏属性 如果输出的时候需要隐藏某些属性,可以使用: ``` <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>); dump(<span class="hljs-regexp">$user</span>->hidden([<span class="hljs-string">'create_time'</span>,<span class="hljs-string">'update_time'</span>])->toArray()); } ``` ``` 再次访问 `http://tp5.com/user/20` 页面输出结果变成: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">array</span> (size=<span class="hljs-number">5</span>) <span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>) <span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>) <span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>) <span class="hljs-string">'status'</span> => int <span class="hljs-number">2</span> ``` ``` ## 指定属性 或者指定一些属性输出,则可以用: ``` <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>); dump(<span class="hljs-regexp">$user</span>->visible([<span class="hljs-string">'id'</span>,<span class="hljs-string">'nickname'</span>,<span class="hljs-string">'email'</span>])->toArray()); } ``` ``` 再次访问 `http://tp5.com/user/20` 页面输出结果变成: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">array</span> (size=<span class="hljs-number">3</span>) <span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>) <span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>) ``` ``` ## 追加属性 如果读取器定义了一些非数据库字段的读取,例如: ``` <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">// status修改器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserStatusAttr</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> ``` ``` 而我们如果需要输出`user_status`属性数据的话,可以使用`append`方法,用法如下: ``` <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>); dump(<span class="hljs-regexp">$user</span>->append([<span class="hljs-string">'user_status'</span>])->toArray()); } ``` ``` 再次访问 `http://tp5.com/user/20` 页面输出结果变成: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">array</span> (size=<span class="hljs-number">8</span>) <span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>) <span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>) <span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>) <span class="hljs-string">'status'</span> => int <span class="hljs-number">2</span><span class="hljs-string">'create_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>) <span class="hljs-string">'update_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>) <span class="hljs-string">'user_status'</span> => string <span class="hljs-string">'待审核'</span> (length=<span class="hljs-number">9</span>) ``` ``` ## 输出JSON 对于`API`开发而言,经常需要返回`JSON`格式的数据,修改`read`操作方法改成`JSON`输出: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 读取用户数据输出JSON</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>->toJson(); } ``` ``` 访问 `http://tp5.com/user/20` 页面输出结果为: ``` <pre class="calibre18"> ``` {"<span class="hljs-regexp">id</span>":<span class="hljs-string"><span class="hljs-number">22</span></span>,"<span class="hljs-regexp">nickname</span>":<span class="hljs-string"><span class="hljs-operator">"张三"</span></span>,"<span class="hljs-regexp">email</span>":<span class="hljs-string"><span class="hljs-operator">"zhanghsan@qq.com"</span></span>,"<span class="hljs-regexp">birthday</span>":<span class="hljs-string"><span class="hljs-operator">"1988\/01\/15"</span></span>,"<span class="hljs-regexp">status</span>":<span class="hljs-string"><span class="hljs-number">2</span></span>,"<span class="hljs-regexp">create_time</span>":<span class="hljs-string"><span class="hljs-operator">"2016-05-02 16:40:57"</span></span>,"<span class="hljs-regexp">update_time</span>":<span class="hljs-string"><span class="hljs-operator">"2016-05-02 16:40:57"</span></span>} ``` ``` 或者采用更简单的方法输出`JSON`,下面的方式是等效的: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 读取用户数据直接输出JSON</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-keyword">echo</span> UserModel::get(<span class="hljs-regexp">$id</span>); } ``` ``` > toJson输出方法仍然支持 `hidden`、`visible`和`append`方法。