隨著web應(yīng)用程序功能的不斷增加,開(kāi)發(fā)人員往往需要花費(fèi)大量時(shí)間來(lái)編寫(xiě)數(shù)據(jù)庫(kù)驗(yàn)證規(guī)則。使用orm(對(duì)象關(guān)系映射)框架,可以將數(shù)據(jù)庫(kù)驗(yàn)證規(guī)則和業(yè)務(wù)邏輯分離,從而節(jié)省了時(shí)間并提高了開(kāi)發(fā)效率。其中,thinkphp6提供了自動(dòng)驗(yàn)證功能,可以幫助我們快速實(shí)現(xiàn)orm自動(dòng)驗(yàn)證數(shù)據(jù)庫(kù)操作。接下來(lái),我們將介紹如何使用thinkphp6實(shí)現(xiàn)orm自動(dòng)驗(yàn)證數(shù)據(jù)庫(kù)操作。
- 安裝thinkphp6
首先,我們需要安裝ThinkPHP6。可以通過(guò)以下命令來(lái)使用composer安裝ThinkPHP6:
composer create-project topthink/think tp6 –prefer-dist
當(dāng)然,也可以到ThinkPHP6的官方網(wǎng)站(https://www.thinkphp.cn)下載最新的框架源代碼。
- 配置數(shù)據(jù)庫(kù)連接
在實(shí)現(xiàn)ORM自動(dòng)驗(yàn)證之前,我們需要先配置數(shù)據(jù)庫(kù)連接。可以在項(xiàng)目根目錄下的config/database.php中進(jìn)行配置,例如:
立即學(xué)習(xí)“PHP免費(fèi)學(xué)習(xí)筆記(深入)”;
return [ // 數(shù)據(jù)庫(kù)類(lèi)型 'type' => 'mysql', // 數(shù)據(jù)庫(kù)連接DSN配置 'dsn' => '', // 服務(wù)器地址 'hostname' => '127.0.0.1', // 數(shù)據(jù)庫(kù)名 'database' => 'test', // 數(shù)據(jù)庫(kù)用戶名 'username' => 'root', // 數(shù)據(jù)庫(kù)密碼 'password' => 'password', // 數(shù)據(jù)庫(kù)連接端口 'hostport' => '3306', // 數(shù)據(jù)庫(kù)連接參數(shù) 'params' => [], // 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8 'charset' => 'utf8', // 數(shù)據(jù)庫(kù)表前綴 'prefix' => '', // 數(shù)據(jù)庫(kù)調(diào)試模式 'debug' => true, // 數(shù)據(jù)庫(kù)部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器) 'deploy' => 0, // 數(shù)據(jù)庫(kù)讀寫(xiě)是否分離 主從式有效 'rw_separate' => false, // 讀寫(xiě)分離后 主服務(wù)器數(shù)量 'master_num' => 1, // 指定從服務(wù)器序號(hào) 'slave_no' => '', // 是否嚴(yán)格檢查字段是否存在 'fields_strict' => true, // 數(shù)據(jù)集返回類(lèi)型 'resultset_type' => 'array', // 是否自動(dòng)寫(xiě)入時(shí)間戳字段 'auto_timestamp' => false, // 是否需要進(jìn)行SQL性能分析 'sql_explain' => false, // 開(kāi)啟斷線重連 'break_reconnect' => true, ];
- 創(chuàng)建和使用模型
在ThinkPHP6中,使用模型來(lái)與數(shù)據(jù)庫(kù)進(jìn)行交互。可以通過(guò)以下命令來(lái)創(chuàng)建一個(gè)模型:
php bin/think make:model Test
這條命令會(huì)在app目錄下創(chuàng)建Test.php文件,其中包含了一個(gè)空的模型類(lèi)Test。我們可以在這個(gè)類(lèi)中指定數(shù)據(jù)庫(kù)操作表名、主鍵名稱(chēng)、自動(dòng)時(shí)間戳等定義。例如:
<?php namespace appmodel; use thinkModel; class Test extends Model { // 指定表名 protected $table = 'test'; // 指定主鍵名 protected $pk = 'id'; // 自動(dòng)時(shí)間戳 protected $autoWriteTimestamp = true; }
在這個(gè)模型類(lèi)中,我們還可以定義一些數(shù)據(jù)驗(yàn)證規(guī)則。例如:
<?php namespace appmodel; use thinkModel; class Test extends Model { // 指定表名 protected $table = 'test'; // 指定主鍵名 protected $pk = 'id'; // 自動(dòng)時(shí)間戳 protected $autoWriteTimestamp = true; // 定義驗(yàn)證規(guī)則 protected $validate=[ 'name'=>'require|max:10', 'age'=>'require|integer|between:1,100', 'email'=>'email', ]; }
在這個(gè)例子中,我們定義了三個(gè)驗(yàn)證規(guī)則:name必需、最大長(zhǎng)度為10;age必需、必需為整數(shù)、取值為1到100之間;email必需為電子郵件格式。這些規(guī)則將在模型數(shù)據(jù)操作時(shí)自動(dòng)進(jìn)行驗(yàn)證。
- 使用自動(dòng)驗(yàn)證功能
在使用模型進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),我們只需要調(diào)用對(duì)應(yīng)的方法即可,例如:
// 添加數(shù)據(jù) $data = [ 'name' => '張三', 'age' => '18', 'email'=> 'zhangsan@test.com', ]; $model = new Test; $result = $model->save($data); if ($result) { echo '數(shù)據(jù)添加成功'; } else { echo '數(shù)據(jù)添加失敗'; } // 更新數(shù)據(jù) $data = [ 'id' => 1, 'name' => '李四', 'age' => '20', 'email'=> 'lisi@test.com', ]; $model = new Test; $result = $model->save($data, ['id' => 1]); if ($result) { echo '數(shù)據(jù)更新成功'; } else { echo '數(shù)據(jù)更新失敗'; } // 刪除數(shù)據(jù) $model = new Test; $result = $model->destroy(1); if ($result) { echo '數(shù)據(jù)刪除成功'; } else { echo '數(shù)據(jù)刪除失敗'; } // 查詢數(shù)據(jù) $model = new Test; $result = $model->where('id', 1)->find(); if ($result) { echo '數(shù)據(jù)查詢成功'; } else { echo '數(shù)據(jù)查詢失敗'; }
在使用這些方法時(shí),ThinkPHP6會(huì)自動(dòng)根據(jù)模型中定義的驗(yàn)證規(guī)則進(jìn)行驗(yàn)證。如果驗(yàn)證失敗,則會(huì)拋出異常并返回失敗原因。
- 自定義驗(yàn)證消息
在自動(dòng)驗(yàn)證中,我們可以通過(guò)在模型類(lèi)中添加靜態(tài)屬性message來(lái)定義驗(yàn)證失敗時(shí)的錯(cuò)誤提示消息。例如:
<?php namespace appmodel; use thinkModel; class Test extends Model { // 指定表名 protected $table = 'test'; // 指定主鍵名 protected $pk = 'id'; // 自動(dòng)時(shí)間戳 protected $autoWriteTimestamp = true; // 定義驗(yàn)證規(guī)則 protected $validate=[ 'name'=>'require|max:10', 'age'=>'require|integer|between:1,100', 'email'=>'email', ]; // 定義驗(yàn)證失敗消息 protected $message = [ 'name.require' => '姓名不能為空', 'name.max' => '姓名長(zhǎng)度不能超過(guò)10個(gè)字符', 'age.require' => '年齡不能為空', 'age.integer' => '年齡必須為整數(shù)', 'age.between' => '年齡取值必須在1到100之間', 'email.email' => ' email格式不正確 ', ]; }
在這個(gè)例子中,我們定義了驗(yàn)證失敗的錯(cuò)誤提示消息。如果檢測(cè)到數(shù)據(jù)驗(yàn)證失敗,ThinkPHP6將根據(jù)定義的消息自動(dòng)返回相應(yīng)的錯(cuò)誤信息。
總結(jié)
通過(guò)使用ThinkPHP6自動(dòng)驗(yàn)證功能,我們可以快速實(shí)現(xiàn)ORM自動(dòng)驗(yàn)證數(shù)據(jù)庫(kù)操作,從而提高開(kāi)發(fā)效率并減少錯(cuò)誤。在模型類(lèi)中定義數(shù)據(jù)驗(yàn)證規(guī)則和消息提示可以幫助我們更加方便地進(jìn)行開(kāi)發(fā)和維護(hù)。希望本篇文章能夠幫助到大家,讓大家在使用ThinkPHP6時(shí)更加得心應(yīng)手。