時間:2023-04-21 07:30:01 | 來源:網(wǎng)站運營
時間:2023-04-21 07:30:01 來源:網(wǎng)站運營
ThinkPHP6 模型基礎操作詳解:表前綴設置:config/database.php
文件里prefix
model
model
創(chuàng)建 Goods.php
文件在模型中除了可以調(diào)用數(shù)據(jù)庫類的方法之外(換句話說,數(shù)據(jù)庫的所有查詢構造器方法模型中都可以支持),可以定義自己的方法,所以也可以把模型看成是數(shù)據(jù)庫的增強版
thinkphp
方法一樣名稱Goods::
也可以用 static::
關鍵詞find
查詢數(shù)據(jù)find
獲取單條數(shù)據(jù),返回的是當前模型的對象實例namespace app/model;use think/Model;class Goods extends Model{ public function find(){ $find = Goods::where('id',7)->find(); return $find; }}
controller
怎么調(diào)用model
namespace app/controller;use app/model/Goods;class Index{ public function index(){ $db = new Goods(); $index = $db->find(); print_r($index); }}
find(主鍵id) 查詢,只使用數(shù)據(jù)表主鍵為id的使用,主鍵非id會查詢失敗
select
查詢數(shù)據(jù)select
獲取多條數(shù)據(jù),返回的是當前模型的對象實例public function select(){ $select = Goods::select(); $select = Goods::select(6); $select = Goods::where('id','>',7)->select(); return $select;}
toArray
方法將當前的模型實例輸出為數(shù)組public function select(){ $select = Goods::select(); $select = Goods::select(6); $select = Goods::where('id','>',7)->select(); return $select->toArray();}
create
靜態(tài)方法添加數(shù)據(jù),返回的是當前模型的對象實例public function create(){ $create = Goods::create([ 'cat' => 3, 'title' => '新商品', 'price' => '59.99', 'add_time' => time() ]); echo $create->id; // 可以直接獲取自增id return $create;}
新增數(shù)據(jù)的最佳實踐原則:使用create方法新增數(shù)據(jù),使用saveAll批量新增數(shù)據(jù)。
update
靜態(tài)方法修改數(shù)據(jù),返回的是當前模型的對象實例save
在取出數(shù)據(jù)后,更改字段更新數(shù)據(jù)。這種方式是最佳的更新方式namespace app/model;use think/Model;class Goods extends Model{ public function update(){ # 更新方式1 $update = Goods::update( ['price'=>'99.99'], ['id'=>22] ); return $update; # 更新方式2 $user = Goods::find(23); $user->price = '102.99'; $save = $user->save(); return $save; }}
delete
靜態(tài)方法刪除數(shù)據(jù),返回的是當前模型的對象實例destroy
根據(jù)主鍵刪除public function delete(){ # 刪除方法1 $delete = Goods::where('id',3)->delete(); # 刪除方法2 $delete = User::destroy(4); return $delete;}
name
和table
class Goods extends Model{ protected $name = 'Admin'; protected $table = 'shop_admin'; public function select(){ $select = Goods::select(); return $select->toArray(); }}
pk
改變主鍵名稱model
默認的主鍵是idclass Goods extends Model{ protected $name = 'Admin'; protected $table = 'shop_admin'; protected $pk = 'uid'; public function find($id=1){ $find = Goods::find($id); return $find->toArray(); }}
schema
設置模型對應數(shù)據(jù)表字段及類型schema
屬性一旦定義,就必須定義完整的數(shù)據(jù)表字段類型class Goods extends Model{ protected $name = 'Goods'; protected $table = 'shop_goods'; protected $pk = 'shop_id'; protected $schema = [ 'shop_id' => 'int', 'cat' => 'int', 'title' => 'string', 'price' => 'float', 'discount' => 'int', 'stock' => 'int', 'status' => 'int', 'add_time' => 'int' ]; # 對某個字段定義需要自動轉換的類型,可以使用type屬性 protected $type = [ 'shop_id' => 'int' ]; public function select(){ $select = Goods::select(); return $select->toArray(); }}
disuse
數(shù)據(jù)表廢棄字段(數(shù)組)class Goods extends Model{ protected $name = 'Goods'; protected $table = 'shop_goods'; protected $pk = 'shop_id'; protected $disuse = [ 'discount', 'stock' ]; public function select(){ $select = Goods::select(); return $select->toArray(); }}
get
+ 字段名 + Attr
class Goods extends Model{ public function index(){ $find = Goods::find(10); echo $find->status; return $find->toArray(); } public function getStatusAttr($v){ $status = [ 1=>'開啟', 2=>'關閉' ]; return $status[$v]; }}
set
+ 字段名 + Attr
class Goods extends Model{ public function index(){ $create = Goods::create([ 'cat' => 3.33, 'title' => '新商品', 'price' => '59.99', 'add_time' => time() ]); return $create; } public function setCatAttr($v,$all){ // $all 全部參數(shù) return (int)$v; }}
search
+ 字段名 + Attr
class Goods extends Model{ public function index(){ $select = Goods::withSearch(['title'],[ 'title' => '新' ])->select(); return $select->toArray(); } public function searchTitleAttr($query,$v){ $query->where('title','like', $v . '%'); }}
empty
判斷isEmpty
方法判斷class Goods extends Model{ public function index(){ $select = Goods::where('title','1')->select(); if(empty($select)){ echo 111; } if($select->isEmpty()){ echo 111; } }}
視頻教程地址:第二十節(jié)thinkphp6模型的基礎配置 關鍵詞:操作,基礎,模型
微信公眾號
版權所有? 億企邦 1997-2025 保留一切法律許可權利。