模型對象和數據對象理解
1. 模型對象:模型類實例化后獲得的對象;
2. 數據對象:獲取到了原始數據的模型對象;
原始數據:存放在模型對象的$data屬性中($data是一個數組)
數據對象:說到底,還是一個模型對象,千萬不要認為是一個全新的對象
DB操作返回是數組。
模型直接操作返回是對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
//該對象共計有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 增加或修改參數
1
|
'resultset_type' => '\think\Collection' , |
即可連貫操作
1
|
model( 'user' )->select()->toArray() |
以上這篇thinkphp5 模型實例化獲得數據對象的教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qazx123q/article/details/79549649