上一篇文章讲解“模型-查询范围”,本篇文章讲解“模型-数组访问和转换”。
1. 数组转换
模型对象支持数组方式访问,例如:
新建Index控制器,并新建arrAccess方法,在方法中以数组形式访问。

浏览器访问(需要新建User模型):

查看数据库

2. 转换为数组
1)toArray方法的使用
可以使用toArray方法将当前的模型实例输出为数组。
在Index控制器中新建transformArr方法

浏览器访问

注意:
1. 查询单条或多条数据均可转换。
2)设置不输出的字段属性——hidden
在Index控制器中新建nonAtt方法

浏览器访问

注意:
1. hidden只针对单条数据。
3)数组输出的字段值会经过获取器的处理,也可以支持追加其它获取器定义(不在数据表字段列表中)的字段。——append
在User模型中新建获取器

在Index控制器中,新建appendAttr方法,输出数组

浏览器访问

注意:
1. append只针对单条数据。
4)设置允许输出的属性——visible
在Index控制器中新建output方法

浏览器访问

5)数据集转为数组
在Index控制器中新建dataArr方法

浏览器访问

ThinkPHP5连载为卓象程序员原创,转载请联系卓象程序员
关注卓象程序员,定期发布技术文章