介紹一個好用的ThinkPHP Repository包

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

介紹

think-repository 是為 thinkphp 6.0.* 提供的存儲庫用于抽象數據層,使我們的應用程序更靈活地進行維護。

你懂的

Thinkphp

>= thinkphp 6.0.*

安裝教程

Composer

composer require fanxd/think-repository dev-master

使用說明

最好在多應用下使用

命令

php think fanxd:repository Post

立即學習PHP免費學習筆記(深入)”;

路由

Route::Resource(‘post’, ‘PostController’);

可用的方法

  • first($id) // 查找單條記錄
  • get() // 查找記錄
  • paginate() // 分頁查詢
  • create($data) // 寫入數據
  • save($data) // 保存當前數據對象
  • delete($where) // 刪除記錄
  • update($where,$data) // 更新記錄
  • find($id) // 查找單條記錄 如果不存在則拋出異常
  • findWhere($where,$columns = [‘*’]) // 指定AND查詢條件 查找單條記錄
  • with([]) // 關聯查詢
  • search([]) // 數據搜索
  • order($order) // 排序

查找記錄

$posts=$this->repository->get();

分頁查詢

$posts=$this->repository->paginate($limit);

按結果按id查找

$posts=$this->repository->find($id);

$posts=$this->repository->first($id);

加載模型關系

$posts=$this->repository->with([‘state’])->find($id);

按結果按字段名查找

$posts=$this->repository->findByField(‘title’, ‘Hello’);

按結果由多個字段查找

$posts=$this->repository->findWhere([
‘id’ => 1], [‘id’, ‘title]);

按結果在一個字段中查找多個值

$posts=$this->repository->findWhereIn(‘id’, [1,2,3,4,5]);

通過排除一個字段中的多個值,按結果查找

$posts=$this->repository->findWhereNotIn(‘id’, [6,7,8,9,10]);

寫入數據

$post = $this->repository->create($data);

更新記錄

$posts=$this->repository->update($where, $data);

刪除記錄

$this->repository->delete($id)

按多個字段刪除存儲庫中的條目

$this->repository->deleteWhere([
‘id’ => 1, ‘user_id’ => 1])

transformer

系統會自動生成transform文件,可自行選擇是否啟用,主要功能對我來說就是美化接口讓我們更專業 ??

<?php  namespace appapitransform;use fanxdrepositorycommandtransformTransform;class PostTransform extends Transform{     public function transform($items)     {         return [             'id'            => $items['id'],             //...              'createTime'    => $items['create_time'],             'updateTime'    => $items['update_time']         ];     }}

陸續添加更多的方法,如果你有好的想法可以告訴我,秒更!!!

相關推薦:最新的10個thinkphp視頻教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享