>  기사  >  PHP 프레임워크  >  유용한 ThinkPHP Repository 패키지 소개

유용한 ThinkPHP Repository 패키지 소개

藏色散人
藏色散人앞으로
2021-07-14 15:15:002295검색

Introduction

think-repository는 데이터 계층을 추상화하여 유지 관리를 위해 애플리케이션을 더욱 유연하게 만들기 위해 thinkphp 6.0.*에 제공되는 저장소입니다. think-repository 是为 thinkphp 6.0.* 提供的存储库用于抽象数据层,使我们的应用程序更灵活地进行维护。

你懂的

ThinkPHP

>= thinkphp 6.0.*

安装教程

Composer

composer require fanxd/think-repository dev-master

使用说明

最好在多应用下使用

命令

php think fanxd:repository Post

路由

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

알고 계시죠

ThinkPHP>= thinkphp 6.0.*

🎜🎜설치 튜토리얼🎜

🎜🎜Composer

🎜🎜composer에는 fanxd/think가 필요합니다 - 저장소 dev-master🎜🎜

🎜🎜지침

🎜🎜여러 애플리케이션에서 사용🎜🎜
🎜🎜명령
🎜🎜php 생각하는 것이 가장 좋습니다 fanxd:repository Post🎜🎜

🎜🎜Route

🎜🎜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로 검색

🎜🎜$ post=$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- > 저장소->업데이트($where, $data);🎜🎜

🎜🎜기록 삭제

🎜🎜$this->저장소->삭제($id)🎜🎜

🎜 🎜저장소의 항목을 삭제하려면 여러 필드를 누르세요.

🎜🎜$this->repository->deleteWhere([
'id' => 1, 'user_id' => 1])🎜 🎜

🎜🎜Transformer

🎜 시스템이 자동으로 transform 파일을 생성합니다. 활성화 여부를 선택할 수 있습니다. 저에게 있어 주요 기능은 인터페이스를 아름답게 만드는 것입니다. professional:)🎜
<?php

namespace app\api\transform;use fanxd\repository\command\transform\Transform;class PostTransform extends Transform{
    public function transform($items)
    {
        return [
            &#39;id&#39;            => $items['id'],
            //... 
            'createTime'    => $items['create_time'],
            'updateTime'    => $items['update_time']
        ];
    }}
🎜🎜 더 많은 방법이 차례로 추가될 예정입니다. 좋은 아이디어가 있으시면 알려주시면 바로 업데이트하겠습니다!!!🎜🎜관련 추천: 🎜최신 10가지 thinkphp 동영상 튜토리얼🎜🎜🎜

위 내용은 유용한 ThinkPHP Repository 패키지 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 learnku.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제