模型對象和數(shù)據(jù)對象理解
1. 模型對象:模型類實例化后獲得的對象;
2. 數(shù)據(jù)對象:獲取到了原始數(shù)據(jù)的模型對象;
原始數(shù)據(jù):存放在模型對象的$data屬性中($data是一個數(shù)組)
數(shù)據(jù)對象:說到底,還是一個模型對象,千萬不要認為是一個全新的對象
DB操作返回是數(shù)組。
模型直接操作返回是對象。
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) { //數(shù)據(jù)庫配置數(shù)組 [ "connection" : protected ] => array (0) { } //數(shù)據(jù)庫查詢對象,負責(zé)最終完成對數(shù)據(jù)庫的操作 [ "query" : protected ] => NULL //模型名稱 ,創(chuàng)建時自動賦值 [ "name" : protected ] => string(5) "Staff" //與模型綁定的數(shù)據(jù)表的完整名稱(包括前綴的表名,如:tp5_staff) [ "table" : protected ] => NULL //用命名空間表示的、當(dāng)前的模型類名:Staff [ "class" : protected ] => string(21) "app\index\model\Staff" //出錯時顯示的信息 [ "error" : protected ] => NULL //字段驗證規(guī)則 [ "validate" : protected ] => NULL //數(shù)據(jù)表主鍵 [ "pk" : protected ] => NULL //數(shù)據(jù)表字段名列表(與數(shù)據(jù)表對應(yīng)) [ "field" : protected ] => array (0) { } //只讀字段列表 [ "readonly" : protected ] => array (0) { } //顯示字段列表 [ "visible" : protected ] => array (0) { } //隱藏屬性字段列表 [ "hidden" : protected ] => array (0) { } //追加屬性列表 [ "append" : protected ] => array (0) { } //與數(shù)據(jù)表字段對應(yīng)的信息列表(極其重要) [ "data" : protected ] => array (0) { } //字段修改信息列表 [ "change" : protected ] => array (0) { } //自動完成列表 [ "auto" : protected ] => array (0) { } //新增自動完成列表 [ "insert" : protected ] => array (0) { } //更新自動完成列表 [ "update" : protected ] => array (0) { } // 是否需要自動寫入時間戳 如果設(shè)置為字符串 則表示時間字段的類型 [ "autoWriteTimestamp" : protected ] => bool(false) //設(shè)置表中:創(chuàng)建時間字段的名稱 [ "createTime" : protected ] => string(11) "create_time" //設(shè)置表中:更新時間字段的名稱 [ "updateTime" : protected ] => string(11) "update_time" //設(shè)置表中:時間字段的格式 [ "dateFormat" : protected ] => string(11) "Y-m-d H:i:s" //數(shù)據(jù)表中各字段類型定義 [ "type" : protected ] => array (0) { } //是否是:更新操作 [ "isUpdate" : protected ] => bool(false) //更新條件 [ "updateWhere" : protected ] => NULL //當(dāng)前執(zhí)行的關(guān)聯(lián)條件 [ "relation" : protected ] => NULL //驗證失敗是否拋出異常 [ "failException" : protected ] => bool(false) //全局查詢范圍設(shè)置 [ "useGlobalScope" : protected ] => bool(true) } |
當(dāng)我們用select()進行查詢得出的結(jié)果無法toarray的時候,下面的方法就用得上了。
對象類型轉(zhuǎn)換數(shù)組
打開 database.php 增加或修改參數(shù)
1
|
'resultset_type' => '\think\Collection' , |
即可連貫操作
1
|
model( 'user' )->select()->toArray() |
以上這篇thinkphp5 模型實例化獲得數(shù)據(jù)對象的教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qazx123q/article/details/79549649