Home  >  Article  >  PHP Framework  >  Introducing a useful ThinkPHP Repository package

Introducing a useful ThinkPHP Repository package

藏色散人
藏色散人forward
2021-07-14 15:15:002361browse

Introduction

think-repository is a repository provided for thinkphp 6.0.* for abstract data layer, Make our applications more flexible for maintenance.

You know

ThinkPHP

>= thinkphp 6.0.*

##Installation Tutorial

Composer

composer require fanxd/think-repository dev-master

Instructions for use

Best

Use

##commands
## under multi-application #php think fanxd:repository Post

##Route

Route::resource('post', 'PostController');

Available methods

first($id) // Find a single record

get() // Find a record
  • paginate() // Pagination query
  • create($data) //Write data
  • save($data) //Save the current data object
  • delete( $where) // Delete record
  • update($where,$data) // Update record
  • find($id) // Find a single record and throw an exception if it does not exist
  • findWhere($where,$columns = ['*']) // Specify AND query conditions to find a single record
  • with([]) //Related query
  • search( []) //Data search
  • order($order) //Sort
  • Find records

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

Paging query

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

Find by result by id

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

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

##Load model relationship

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

Press Results are found by field name

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

Find by multiple fields

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


Find multiple values ​​in one field by result

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

Find by results by excluding multiple values ​​in a field

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

Write data

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

Update record

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

##Delete record

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

Delete entries in the repository by multiple fields

$this->repository->deleteWhere([

'id' => 1, 'user_id' => 1])

Transformer

The system will automatically generate the
transform

file. You can choose whether to enable it. The main function for me is to beautify the interface and make us more 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']
        ];
    }}

More methods will be added one after another. If you have good ideas, please tell me and I will update them in seconds!!!

Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of Introducing a useful ThinkPHP Repository package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete