## 模型初始化 initialize( )
#### 1. 作用:創建模型對象之前做的預處理工作。
>[info] 在ThinkPHP5中,創建數據模型是一個非常容易的過程,整個過程是透明的。
#### 2. 實例演示:創建模型對象前,初始化部分模型類屬性
>[info] 模型對象創建時,除了模型名$name和所繼承的類$class(命名空間表示)之外,其它所有的模型屬性都是空的或只有默認值。隨著以后的操作,這些屬性會根據操作要求自動完成初始化。
* 第一步:在上節課創建的模型類Staff.php里,添加初始化方法
>[info] 本教程如無特別說明:源碼均指:Model.php 類文件中的內容。
文件位置:/thinkphp/library/think/Model.php
~~~
<?php
namespace app\index\model;
//導入模型類
use think\model;
class Staff extends model {
//在子類重寫父類的初始化方法initialize()
protected function initialize(){
//繼承父類中的initialize()
parent::initialize();
//初始化數據表名稱,通常自動獲取不需設置
$this->table = 'tp5_staff';
//初始化數據表字段信息
$this->field = $this->db()->getTableInfo('', 'fields');
//初始化數據表字段類型
$this->type = $this->db()->getTableInfo('', 'type');
//初始化數據表主鍵
$this->pk = $this->db()->getTableInfo('', 'pk');
}
}
~~~
>[info] 在自定義的模型初始化方法中,我們對模型中的相關屬性進行初始化:$field,$pk,$table,$type(屬性含義上節課已詳細說明)
* 第二步:控制器Index.php中進行調用:
~~~
<?php
namespace app\index\controller;
//導入模型類
use app\index\model\Staff;
class Index {
public function index(){
//創建模型類Staff
$model = new Staff();
//查看Staff類實例$model
dump($model);
}
}
~~~
* 第三步:瀏覽器地址欄輸入:tp5.com/index.php/index/index/ 查看這個自定義模型對象
~~~
object(app\index\model\Staff)#5 (28) {
["connection":protected] => array(0) {
}
["query":protected] => NULL
["name":protected] => string(5) "Staff"
//這是我們初始化獲取的數據表名稱
["table":protected] => string(9) "tp5_staff"
["class":protected] => string(21) "app\index\model\Staff"
["error":protected] => NULL
["validate":protected] => NULL
//這是我們初始化獲取的數據表主鍵信息
["pk":protected] => string(2) "id"
//這是我們初始化獲取的數據表字段名信息
["field":protected] => array(7) {
[0] => string(2) "id"
[1] => string(4) "name"
[2] => string(3) "sex"
[3] => string(3) "age"
[4] => string(6) "salary"
[5] => string(4) "dept"
[6] => string(8) "hiredate"
}
["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(7) {
["id"] => string(15) "int(4) unsigned"
["name"] => string(11) "varchar(30)"
["sex"] => string(19) "tinyint(2) unsigned"
["age"] => string(19) "tinyint(3) unsigned"
["salary"] => string(19) "float(8,2) unsigned"
["dept"] => string(19) "tinyint(2) unsigned"
["hiredate"] => string(4) "date"
}
["isUpdate":protected] => bool(false)
["updateWhere":protected] => NULL
["relation":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
}
~~~
* * * * *
#### 3、總結:
>[success] 模型初始化,絕大多數情況下會自動完成,并不需要我們去做。
- 前言[隨時更新]
- 開發環境
- 1.Mac環境
- 2.windows環境
- 模型對象
- 1.創建模型對象
- 2.模型初始化
- 數據對象
- 1.定義數據對象
- 2.創建數據對象
- 1.data方法
- 2.setAttr方法
- 3.__set方法
- 4.查詢數據對象
- 1.getData方法
- 2.getAttr方法
- 3.__get方法
- OOP難點總結
- 1.get_class( )實例講解
- 2.get_called_class( )實例講解
- 3.__call( )實例講解
- 3.__callStatic( )實例講解
- 4.call_user_func_array函數[重點]
- 5.普通方法與靜態方法
- 6.在Model源碼中的應用
- 7.new static 延遲靜態綁定
- PHP標準化規范
- 查詢數據
- 1.獲取單條:get靜態方法
- 2.獲取單條:對象查詢
- 3.獲取多條:all靜態方法
- 4.獲取多條:對象查詢
- 5.獲取字段值:value方法
- 6.獲取列值:column方法
- 7.動態查詢:getBy字段名
- 8.助手函數:model查詢
- 9.加載器:Loader類查詢
- 10.數據庫與模型查詢對比
- 新增數據
- 1.sava方法
- 2.savaAll方法
- 3.create靜態方法
- 4.insert靜態調用
- 更新數據
- 1.單條更新:save方法
- 2.批量更新:saveAll方法
- 3.靜態更新:update方法
- 4.查詢類Query直接更新
- 5. 閉包更新
- 刪除數據
- 1.刪除當前記錄:delete
- 2.靜態條件刪除:destory
- 獲取器
- 1.模型方法:set屬性Attr
- 修改器
- 1.set屬性Attr
- 時間戳
- 1.MySQL中日期類型復習
- 2.時間戳功能詳解
- 軟刪除[重點]
- 1.traits詳解[選學內容]
- 2.SoftDelet類源碼分析
- 3. delete實例刪除
- 4.destroy條件刪除
- 5.restore恢復數據
- 類型轉換
- 1. 規則設置
- 2. 實例演示
- 查詢范圍
- 1. 基本概念
- 2.實例演示