模型对象和数据对象理解
1. 模型对象:模型类实例化后获得的对象;
2. 数据对象:获取到了原始数据的模型对象;
原始数据:存放在模型对象的$data属性中($data是一个数组)
数据对象:说到底,还是一个模型对象,千万不要认为是一个全新的对象
DB操作返回是数组。
模型直接操作返回是对象。
- //该对象共计有28个受保护属性,必须在本类或子类中使用,外部不能直接使用
- object(app\index\model\Staff)#5 (28) {
-
- //数据库配置数组
- ["connection":protected] => array(0) {
- }
- //数据库查询对象,负责最终完成对数据库的操作
- ["query":protected] => NULL
- //模型名称 ,创建时自动赋值
- ["name":protected] => string(5) "Staff"
- //与模型绑定的数据表的完整名称(包括前缀的表名,如:tp5_staff)
- ["table":protected] => NULL
- //用命名空间表示的、当前的模型类名:Staff
- ["class":protected] => string(21) "app\index\model\Staff"
- //出错时显示的信息
- ["error":protected] => NULL
- //字段验证规则
- ["validate":protected] => NULL
- //数据表主键
- ["pk":protected] => NULL
- //数据表字段名列表(与数据表对应)
- ["field":protected] => array(0) {
- }
- //只读字段列表
- ["readonly":protected] => array(0) {
- }
- //显示字段列表
- ["visible":protected] => array(0) {
- }
- //隐藏属性字段列表
- ["hidden":protected] => array(0) {
- }
- //追加属性列表
- ["append":protected] => array(0) {
- }
- //与数据表字段对应的信息列表(极其重要)
- ["data":protected] => array(0) {
- }
- //字段修改信息列表
- ["change":protected] => array(0) {
- }
- //自动完成列表
- ["auto":protected] => array(0) {
- }
- //新增自动完成列表
- ["insert":protected] => array(0) {
- }
- //更新自动完成列表
- ["update":protected] => array(0) {
- }
- // 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
- ["autoWriteTimestamp":protected] => bool(false)
- //设置表中:创建时间字段的名称
- ["createTime":protected] => string(11) "create_time"
- //设置表中:更新时间字段的名称
- ["updateTime":protected] => string(11) "update_time"
- //设置表中:时间字段的格式
- ["dateFormat":protected] => string(11) "Y-m-d H:i:s"
- //数据表中各字段类型定义
- ["type":protected] => array(0) {
- }
- //是否是:更新操作
- ["isUpdate":protected] => bool(false)
- //更新条件
- ["updateWhere":protected] => NULL
- //当前执行的关联条件
- ["relation":protected] => NULL
- //验证失败是否抛出异常
- ["failException":protected] => bool(false)
- //全局查询范围设置
- ["useGlobalScope":protected] => bool(true)
- }
当我们用select()进行查询得出的结果无法toarray的时候,下面的方法就用得上了。
对象类型转换数组
打开 database.php 增加或修改参数
- 'resultset_type' => '\think\Collection',
即可连贯操作
- model('user')->select()->toArray()
以上这篇thinkphp5 模型实例化获得数据对象的教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。