數據模型
~~~
<?php
namespace app\index\model;
use think\Model;
class Staff extends Model {
//Model 均為大寫
}
?>
~~~
模型會自動對應數據表,模型類的命名規則是除去表前綴的數據表名稱,采用駝峰法命名,并且首字母大寫,例如:
|模型名 |約定對應數據表(假設數據庫的前綴定義是 think_)|
|---|---|
|User| think_user|
|UserType| think_user_type|
控制器
~~~
<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index {
public function index(){
$model = new Staff();
dump($model);
}
}
~~~
#### 輸出反饋
//該對象共計有32個受保護屬性,必須在本類或子類中使用,外部不能直接使用
~~~
object(app\index\model\Staff)#5 (32) {
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["name":protected] => string(5) "Staff"
["table":protected] => NULL
["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) {
}
["origin":protected] => array(0) {
}
["relation":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
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
~~~
目前創建的模型雖然按相關規則,已經與特定數據表綁定了,但是該類的絕大多數屬性的值,仍處于默認或不確定狀態;
現在該模型中有二個屬性值是確定的:
1. $name //模型名稱
2. $class //模型類命名空間,即如何找到這個類
其它的屬性值,在創建數據對象時,會自動獲取。
指定數據表甚至數據庫連接
~~~
namespace app\index\model;
class User extends \think\Model
{
// 設置當前模型對應的完整數據表名稱
protected $table = 'think_user';
// 設置當前模型的數據庫連接
protected $connection = [
// 數據庫類型
'type' => 'mysql',
// 服務器地址
'hostname' => '127.0.0.1',
// 數據庫名
'database' => 'thinkphp',
// 數據庫用戶名
'username' => 'root',
// 數據庫密碼
'password' => '',
// 數據庫編碼默認采用utf8
'charset' => 'utf8',
// 數據庫表前綴
'prefix' => 'think_',
// 數據庫調試模式
'debug' => false,
];
}
~~~
和連接數據庫的參數一樣,connection屬性的值也可以設置為數據庫的配置參數,而且也是官方推薦的方式,這樣可以避免把數據庫連接固化在代碼里面。
5.0不支持單獨設置當前模型的數據表前綴。
- 目錄
- 5.0.10環境配置
- 5.0.10控制器模型對象
- 5.0.10模型初始化
- 5.0.10定義數據對象
- 5.0.10創建數據對象data()方法
- 5.0.10創建數據對象-setAttr方法
- 5.0.10創建數據對象__set()方法
- 5.0.10查詢數據對象getData()
- 實例
- 5.0.10保存數據save()方法
- 5.0.10保存數據saveAll()
- 5.0.10更新數據save()
- 5.0.10批量更新數據saveAll()
- 5.0.10刪除數據delete()
- 5.0.10刪除數據destroy()
- 5.0.10traits詳解
- ThinkPHP 5.0 速查表
- 注釋
- 環境變量配置
- Model分層
- MVC 邏輯服務數據
- Model分層及多對多關聯的建立
- 控制器調用邏輯層
- Session
- 子域名session共享
- 系統錯誤
- 版本錯誤
- 返回錯誤
- Token令牌及身份識別
- 關聯查詢
- 安裝
- Git安裝
- Composer
- 擴展
- Composer類
- 非Composer類
- 引用第三方庫
- 自定義命令行
- 計劃任務
- 調試
- 調試模式
- 監聽SQL
- 數據庫調試
- 單元測試
- 初始化
- 控制器初始化
- 模型初始化
- 數據緩存
- 實戰
- 版本升級
- 從V5.0.17升級到V5.0.18