agungsugiarto/codeigniter4-repository
Composer 安装命令:
composer require agungsugiarto/codeigniter4-repository
包简介
Implementation of repository pattern for CodeIgniter 4. The package allows out-of-the-box filtering of data based on parameters in the <b>request</b>, and also allows you to quickly integrate the list filters and custom criteria.
README 文档
README
About
Implementation of repository pattern for CodeIgniter 4. The package allows out-of-the-box filtering of data based on parameters in the request, and also allows you to quickly integrate the list filters and custom criteria.
Table of Contents
Installation
Via Composer
$ composer require agungsugiarto/codeigniter4-repository
Overview
Package allows you to filter data based on incoming request parameters:
https://example.com/news?title=Title&custom=value&orderBy=name_desc
It will automatically apply built-in constraints onto the query as well as any custom scopes and criteria you need:
protected $searchable = [ // where 'title' equals 'Title' 'title', ]; protected $scopes = [ // and custom parameter used in your scope 'custom' => MyScope::class, ];
class MyScope extends ScopeAbstract { public function scope($builder, $value, $scope) { return $builder->where($scope, $value)->orWhere(...); } }
Ordering by any field is available:
protected $scopes = [ // orderBy field 'orderBy' => OrderByScope::class, ];
Package can also apply any custom criteria:
return $this->news->withCriteria([ new MyCriteria([ 'category_id' => '1', 'name' => 'Name', ['created_at', '>', Time::now()], ]), ... ])->get();
Usage
Create a Model
Create your model:
namespace App\Models; use CodeIgniter\Model; class News extends Model { ... }
Create a Repository
Extend it from Fluent\Repository\Eloquent\BaseRepository and provide entity() method to return full model class name:
namespace App; use App\Models\News; use Fluent\Repository\Eloquent\BaseRepository; class NewsRepository extends BaseRepository { public function entity() { // Whatever choose one your style. return new News(); // or return 'App\Models\News'; // or return News::class; } }
Use built-in methods
use App\NewsRepository; class NewsController extends BaseController { protected $news; public function __construct() { $this->news = new NewsRepository(); } .... }
Available methods
- Execute the query as a "select" statement or get all results:
/** * Get method implement parameter "select", "limit" and "offset". * The default will be select * and return all offset data. * * Example: $this->news->get(['*'], 50, 100); */ $news = $this->news->get();
- Execute the query and get the first result:
$news = $this->news->first();
- Find a model by its primary key:
$news = $this->news->find(1);
- Add basic where clauses and execute the query:
$news = $this->news->findWhere([ // where id equals 1 'id' => '1', // other "where" operations ['news_category_id', '<', '3'], ... ]);
- Paginate the given query:
Note:
"paginate": {}avaliable methods see docs
$news = $this->news->paginate(15); // return will be { "data": [ { "id": "3", "title": "Ms. Carole Wilderman DDS", "content": "Labore id aperiam ut voluptatem eos natus.", "created_at": "2020-08-05 17:07:16", "updated_at": "2020-08-05 17:07:16", "deleted_at": null }, ... ], "paginate": {} }
- Add an "order by" clause to the query:
$news = $this->news->orderBy('title', 'desc')->get();
- Save a new model and return the instance:
$news = $this->news->create($this->request->getVar());
- Save a batch new model and return instance:
$data = [ [ 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date' ], [ 'title' => 'Another title', 'name' => 'Another Name', 'date' => 'Another date' ] ]; $news = $this->news->createBatch($data);
- Update a record:
$this->news->update($this->request->getVar(), $id);
- Update a batch record:
$data = [ [ 'title' => 'My title', 'name' => 'My Name', 'date' => 'My date' ], [ 'title' => 'Another title', 'name' => 'Another Name', 'date' => 'Another date' ] ]; $news = $this->news->updateBatch($data, 'title');
- Delete a record by id:
$this->news->destroy($id);
Create a Criteria
Criteria are a way to build up specific query conditions.
use Fluent\Repository\Contracts\CriterionInterface; class MyCriteria implements CriterionInterface { protected $conditions; public function __construct(array $conditions) { $this->conditions = $conditions; } public function apply($entity) { foreach ($this->conditions as $field => $value) { $entity = $entity->where($field, $value); } return $entity; } }
Multiple Criteria can be applied:
use App\NewsRepository; class NewsController extends BaseController { protected $news; public function __construct() { $this->news = new NewsRepository(); } public function index() { return $this->news->withCriteria([ new MyCriteria([ 'category_id' => '1', 'name' => 'Name' ]), new WhereAdmin(), ... ])->get(); } }
Scope, Filter and Order
In your repository define which fields can be used to scope your queries by setting $searchable property.
protected $searchable = [ // where 'title' equals parameter value 'title', // orWhere equals 'body' => 'or', // where like 'author' => 'like', // orWhere like 'email' => 'orLike', ];
Search by searchables:
public function index() { return $this->news->scope($this->request)->get(); }
https://example.com/news?title=Title&body=Text&author=&email=gmail
Also several serchables enabled by default:
protected $scopes = [ // orderBy field 'orderBy' => OrderByScope::class, // where created_at date is after 'begin' => WhereDateGreaterScope::class, // where created_at date is before 'end' => WhereDateLessScope::class, ];
$this->news->scope($this->request)->get();
Enable ordering for specific fields by adding $orderable property to your model class:
public $orderable = ['email'];
https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26
orderBy=email_desc will order by email in descending order, orderBy=email - in ascending
You can also build your own custom scopes. In your repository override scope() method:
public function scope(IncomingRequest $request) { // apply build-in scopes parent::scope($request); // apply custom scopes $this->entity = (new NewsScopes($request))->scope($this->entity); return $this; }
Create your scopes class and extend ScopesAbstract
use Fluent\Repository\Scopes\ScopesAbstract; class NewsScopes extends ScopesAbstract { protected $scopes = [ // here you can add field-scope mappings 'field' => MyScope::class, ]; }
Now you can build any scopes you need:
use Fluent\Repository\Scopes\ScopeAbstract; class MyScope extends ScopeAbstract { public function scope($builder, $value, $scope) { return $builder->where($scope, $value); } }
License
Released under the MIT License, see LICENSE.
agungsugiarto/codeigniter4-repository 适用场景与选型建议
agungsugiarto/codeigniter4-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.07k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2020 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「repository」 「filters」 「criteria」 「repository-pattern」 「codeigniter4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 agungsugiarto/codeigniter4-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 agungsugiarto/codeigniter4-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 agungsugiarto/codeigniter4-repository 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Simple filters for laravel
A Laravel package for the Repository Design Pattern.
Livewire Filters is a series of Livewire components that provide you with the tools to do live filtering of your data from your own Livewire components.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 1.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-16