溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除

發(fā)布時(shí)間:2021-03-11 16:30:10 來源:億速云 閱讀:415 作者:TREX 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

  • 使用model 查詢數(shù)據(jù),添加數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù)

  • 聚合操作

  • 獲取器,修改器

  • 自動添加時(shí)間戳(創(chuàng)建時(shí)間,修改時(shí)間)

  • 軟刪除

1、使用model查詢數(shù)據(jù)

$res = User::get(1); //獲取主鍵為1的數(shù)據(jù),得到的是一個(gè)對象
 
$res = $res->toArray(); //將對象轉(zhuǎn)化為數(shù)組
 
dump($res->name); //獲取 $res 里 name 字段的值
//使用閉包函數(shù)查詢 id=1 的記錄
$res = User::get(function($query){
  $query->where("id","eq",1)
     ->field('name')
});
$res = User::where("id",10)->value('name');
$res = User::where("id",10)->field('name')->find();
 
$res = User::column('email');        //查詢所有的 email 字段值    
$res = User::where("id",">",5)->select();  //查詢所有id大于5的記錄
$res = User::all('1,2'); //查詢主鍵等于 1 或2 的記錄
foreach($res as $val)  //轉(zhuǎn)化為數(shù)組
{
  dump($val->toArray());
}
//使用閉包函數(shù)查詢 id<5 的記錄
$res = User::get(function($query){
  $query->where("id","<",5)
     ->field('name')
});

2、使用model添加數(shù)據(jù)

$res = User::create([
  'name' => 'yulong',
  'pwd' => '123'
],true);         //第二個(gè)參數(shù)為true時(shí),只添加數(shù)據(jù)表中已有的字段,不報(bào)錯(cuò),不寫則默認(rèn)為false;;;true 也可以換成一個(gè)數(shù)組,數(shù)組里存放數(shù)據(jù)表中的字段,表示僅允許數(shù)組中的字段添加數(shù)據(jù)
$res->id; //本次添加的自增id
dump($res);
$usermodel = new User;
$res = $usermodel
  ->allowField(true) //僅允許添加數(shù)據(jù)表中存在的字段,也可以寫成數(shù)組
  ->save([
    'name' => 'yulong',
    'pwd' => '123'
  ]);
 
dump($res->id); //獲取新添加數(shù)據(jù)的自增id
$usermodel = new User;
$res = $usermodel->saveAll([  //一次保存多條數(shù)據(jù)
  'name' => 'yulong001',
  'name' => 'yulong002'
]);
 
dump($ers);

3、使用model更新數(shù)據(jù)

$res = User::update([
  'name' => 'yulong002'
],['id'=>1]);        //更新 id=1 的記錄
 
 $res = User::update([
  'name' => 'yulong002'
],function(){
  $query->where("id","LT",5);  //使用閉包函數(shù)更新 id<5 的記錄
}); 
 
 
dump($res);
$res = User::where("id","<",6)   //返回值是被更新數(shù)據(jù)的行數(shù)
    ->update([
       'name' => 'hahahaha'
      ]);

4、使用model刪除數(shù)據(jù)

$res = User::destriy(1);  //刪除主鍵為1的記錄,返回影響數(shù)據(jù)的行數(shù),也可以傳遞數(shù)組
 
 
$usermodel = User::get(1);
$res    = $usermodel->delete();
 
 
$res = User::where("id",5)->delete(); // where() 里面有三個(gè)參數(shù), 字段值,條件,數(shù)值
 
dump($res);

5、使用model聚合操作

$res = User::where("id",">",5)->count(); //查詢id大于5的記錄條數(shù)
 
// max 可以換成其他的 如 min / sum / avg
$res = User::max('num');         //查詢 num 字段中的最大值
$res = User::where("id","<",5)->max('num'); //id<5 的記錄中的 num 最大值

6、使用模型獲取器

//model 
//方法名: get字段名Attr
//controller中獲取原始數(shù)據(jù)使用 $res->getData()
 
public function getSexSttr($val){
  switch($val){
    case '1':
      return "男";
      break;
    case '2';
      return '女';
      break;
    default:
      return '未知';
      break;
  }
}

7、使用模型修改器

//model 修改器命名 set字段名Attr
//修改器作用:在往數(shù)據(jù)庫添加字段時(shí),控制器中寫未處理的數(shù)據(jù),在模型中的修改器中寫處理數(shù)據(jù)的方法,這樣添加到數(shù)據(jù)庫中的數(shù)據(jù)就是處理過得數(shù)據(jù)了
public function setPwdAttr($val){
  return md5($val);
}
 
// $val代表 pwd 字段,$data代表接收到的所有數(shù)據(jù) ,返回的值就是 pwd+email 
public function setPwdAttr($val,$data){
  return $val.$data['email'];
}

8、自動往數(shù)據(jù)庫中添加時(shí)間戳

//自動往 time 字段中加入時(shí)間戳
public function setTimeAttr(){
  return time();
}
 
//在數(shù)據(jù)添加時(shí)發(fā)生改變
protected $insert = [ 'time_insert' ]; //設(shè)置字段
public function setTimeInsertAttr(){  //將字段值設(shè)置為當(dāng)前時(shí)間
  return time();
}
 
//在更新數(shù)據(jù)時(shí)發(fā)生改變
protected $update = [ 'time_update' ]; //設(shè)置字段
public function setTimeUpdateAttr(){  //將字段值設(shè)置為當(dāng)前時(shí)間
  return time();
}

9、model時(shí)間戳

// 數(shù)據(jù)庫中的字段 create_time update_time
// database.php 中更改配置 'auto_timeStamp' => true
// 不推薦使用此方法,因?yàn)槿绻愕臄?shù)據(jù)庫表中沒有 對應(yīng)的字段 ,程序可能就會報(bào)錯(cuò)
// 可以單獨(dú)在 某個(gè)模型中 添加屬性 
 
protected $autoWriteTimeStamp = true; //開啟自動加入時(shí)間戳
 
protected $createTime = 'create_at';  //設(shè)置 創(chuàng)建的時(shí)候?qū)懭?nbsp;的字段 ,值可以為false,關(guān)閉操作
protedted $updateTime = 'update_at';  //設(shè)置 創(chuàng)建和更新的時(shí)候?qū)懭?nbsp;的字段 ,值可以為false,關(guān)閉操作

10、軟刪除

// model
// 數(shù)據(jù)表中的字段 delete_time,默認(rèn)值可以為 null
 
use traits\model\SoftDelete; //使用軟刪除的類
 
class User extends Model
{
  use SoftDelete;      //在類的開頭 use SoftDelete; 
  protected $deleteTime = 'delete_at';  //設(shè)置軟刪除的字段,默認(rèn)為 delete_time
}
 
 
$res = User::destroy(3,true); //刪除主鍵為3的記錄,第二個(gè)參數(shù)為 true 時(shí),不是軟刪除,是tm真刪了
 
$ress = User::get(4);
$res = $ress->delete(true); // delete() 沒值時(shí),為軟刪除;值為true,tm的真刪
 
// controller 獲取到 軟刪除 的記錄
$res = User::withTrashed(true)->find(1); //得到id為1 的經(jīng)過軟刪除 刪除的記錄
dump($res->getData()); //獲取原始數(shù)據(jù)
 
$res = User::onlyTrashed()->select(); //獲取所有軟刪除的數(shù)據(jù)

“TP5框架model怎么增刪改查、聚合、時(shí)間戳、軟刪除”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI