国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > ThinkPHP6 模型基礎操作詳解

ThinkPHP6 模型基礎操作詳解

時間:2023-04-21 07:30:01 | 來源:網(wǎng)站運營

時間:2023-04-21 07:30:01 來源:網(wǎng)站運營

ThinkPHP6 模型基礎操作詳解:

ThinkPHP6 模型

一、創(chuàng)建模型

表前綴設置:config/database.php 文件里 prefix

二、模型操作

在模型中除了可以調(diào)用數(shù)據(jù)庫類的方法之外(換句話說,數(shù)據(jù)庫的所有查詢構造器方法模型中都可以支持),可以定義自己的方法,所以也可以把模型看成是數(shù)據(jù)庫的增強版

1、find查詢數(shù)據(jù)

namespace app/model;use think/Model;class Goods extends Model{ public function find(){ $find = Goods::where('id',7)->find(); return $find; }}

2、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會查詢失敗

3、select查詢數(shù)據(jù)

public function select(){ $select = Goods::select(); $select = Goods::select(6); $select = Goods::where('id','>',7)->select(); return $select;}

4、數(shù)據(jù)轉換

public function select(){ $select = Goods::select(); $select = Goods::select(6); $select = Goods::where('id','>',7)->select(); return $select->toArray();}

5、增加數(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ù)。

6、修改數(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; }}

7、刪除數(shù)據(jù)

public function delete(){ # 刪除方法1 $delete = Goods::where('id',3)->delete(); # 刪除方法2 $delete = User::destroy(4); return $delete;}

三、模型設置

1、nametable

class Goods extends Model{ protected $name = 'Admin'; protected $table = 'shop_admin'; public function select(){ $select = Goods::select(); return $select->toArray(); }}

2、pk 改變主鍵名稱

class 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(); }}

3、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(); }}

4、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(); }}

四、模型 主要功能

1、獲取器

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]; }}

2、修改器

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; }}

3、搜索器

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 . '%'); }}

4、檢查數(shù)據(jù)

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模型的基礎配置



關鍵詞:操作,基礎,模型

74
73
25
news

版權所有? 億企邦 1997-2025 保留一切法律許可權利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉