thinkphp5 模型实例化 获得数据对象

模型对象和数据对象理解

1. 模型对象:模型类实例化后获得的对象;

2. 数据对象:获取到了原始数据的模型对象;

原始数据:存放在模型对象的$data属性中($data是一个数组)

数据对象:说到底,还是一个模型对象,千万不要认为是一个全新的对象

DB操作返回是数组。
模型直接操作返回是对象。

[plain]  view plain  copy
  1. //该对象共计有28个受保护属性,必须在本类或子类中使用,外部不能直接使用  
  2. object(app\index\model\Staff)#5 (28) {  
  3.   
  4.   //数据库配置数组  
  5.   ["connection":protected] => array(0) {  
  6.   }  
  7.   //数据库查询对象,负责最终完成对数据库的操作  
  8.   ["query":protected] => NULL  
  9.   //模型名称 ,创建时自动赋值  
  10.   ["name":protected] => string(5) "Staff"  
  11.   //与模型绑定的数据表的完整名称(包括前缀的表名,如:tp5_staff)  
  12.   ["table":protected] => NULL  
  13.   //用命名空间表示的、当前的模型类名:Staff  
  14.   ["class":protected] => string(21) "app\index\model\Staff"  
  15.   //出错时显示的信息  
  16.   ["error":protected] => NULL  
  17.   //字段验证规则  
  18.   ["validate":protected] => NULL  
  19.   //数据表主键  
  20.   ["pk":protected] => NULL  
  21.   //数据表字段名列表(与数据表对应)  
  22.   ["field":protected] => array(0) {  
  23.   }  
  24.   //只读字段列表  
  25.   ["readonly":protected] => array(0) {  
  26.   }  
  27.   //显示字段列表  
  28.   ["visible":protected] => array(0) {  
  29.   }  
  30.   //隐藏属性字段列表  
  31.   ["hidden":protected] => array(0) {  
  32.   }  
  33.   //追加属性列表  
  34.   ["append":protected] => array(0) {  
  35.   }  
  36.   //与数据表字段对应的信息列表(极其重要)  
  37.   ["data":protected] => array(0) {  
  38.   }  
  39.   //字段修改信息列表  
  40.   ["change":protected] => array(0) {  
  41.   }  
  42.   //自动完成列表  
  43.   ["auto":protected] => array(0) {  
  44.   }  
  45.   //新增自动完成列表  
  46.   ["insert":protected] => array(0) {  
  47.   }  
  48.   //更新自动完成列表  
  49.   ["update":protected] => array(0) {  
  50.   }  
  51. // 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型  
  52.   ["autoWriteTimestamp":protected] => bool(false)  
  53.   //设置表中:创建时间字段的名称  
  54.   ["createTime":protected] => string(11) "create_time"  
  55.     //设置表中:更新时间字段的名称  
  56.   ["updateTime":protected] => string(11) "update_time"  
  57.     //设置表中:时间字段的格式  
  58.   ["dateFormat":protected] => string(11) "Y-m-d H:i:s"  
  59.   //数据表中各字段类型定义  
  60.   ["type":protected] => array(0) {  
  61.   }  
  62.   //是否是:更新操作  
  63.   ["isUpdate":protected] => bool(false)  
  64.   //更新条件  
  65.   ["updateWhere":protected] => NULL  
  66.   //当前执行的关联条件  
  67.   ["relation":protected] => NULL  
  68.   //验证失败是否抛出异常  
  69.   ["failException":protected] => bool(false)  
  70. //全局查询范围设置  
  71.   ["useGlobalScope":protected] => bool(true)  
  72. }  
当我们用select()进行查询得出的结果无法toarray的时候,下面的方法就用得上了。
对象类型转换数组
打开 database.php 增加或修改参数
'resultset_type' => '\think\Collection',
即可连贯操作

model('user')->select()->toArray()

猜你喜欢

转载自blog.csdn.net/m0_37412958/article/details/80206774
今日推荐