定制 giordanolima/eloquent-repository 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

giordanolima/eloquent-repository

Composer 安装命令:

composer require giordanolima/eloquent-repository

包简介

Repository pattern for Eloquent ORM with a powerfull cache for database queries.

README 文档

README

Latest Stable Version Total Downloads License StyleCI

Documentação em português.

Package to assist the implementation of the Repository Pattern using Eloquent ORM. The main feature is the flexibility and ease of use and a powerful driver for query caching.

Installation

Installing via Composer

composer require giordanolima/eloquent-repository

To configure the package options, declare the Service Provider in the config / app.php file.

'providers' => [
    ...
    GiordanoLima\EloquentRepository\RepositoryServiceProvider::class,
],

If you are using version 5.5 or higher of Laravel, the Service Provider is automatically recognized by Package Discover.

To publish the configuration file:

php artisan vendor:publish

Usage

To get started you need to create your repository class and extend the BaseRepository available in the package. You also have to set the Model that will be used to perform the queries. Example:

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
}

Now it is possible to perform queries in the same way as it is used in Elquent.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
    
    public function getAllUser(){
        return $this->all();
    }
    
    public function getByName($name) {
        return $this->where("name", $name)->get();
    }
    
    // You can create methods with partial queries
    public function filterByProfile($profile) {
        return $this->where("profile", $profile);
    }
    
    // Them you can use the partial queries into your repositories
    public function getAdmins() {
        return $this->filterByProfile("admin")->get();
    }
    public function getEditors() {
        return $this->filterByProfile("editor")->get();
    }
    
    // You can also use Eager Loading in queries
    public function getWithPosts() {
        return $this->with("posts")->get();
    }
}

To use the class, just inject them into the controllers.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    protected function index(UserRepository $repository) {
        return $repository->getAdmins();
    }
}

The injection can also be done in the constructor to use the repository in all methods.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }
    
    public function index() {
        return $this->repository->getAllUsers();
    }
    
}

The Eloquent/QueryBuilder methods are encapsulated as protected and are available just into the repository class. Declare your own public data access methods within the repository to access them through the controller.

Paginate

As default value, the paginate method uses, 15 records per page. This default value can be set in the configuration file to be used in all repositories. If necessary, you can change the default value for a single repository overwriting the perPage property with the desired value.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $perPage = 10;
    protected function model() {
        return \App\User::class;
    }
}

OrderBy

You can declare a field and a default direction to be used for all queries in a particular repository. You can still choose other fields to sort or just skip the sorting methods.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $orderBy = "created_at";
    protected $orderByDirection = "DESC";
	protected function model() {
        return \App\User::class;
    }
    
    public function getAllUser(){
        // This query will use the default ordering of the repository.
        return $this->all();
    }
    
    public function getByName($name) {
        // In this query only the declared sort will be used.
        return $this->orderBy("name")->where("name", $name)->get();
    }
    
    // É possível criar métodos com consultas parciais
    public function getWithoutOrder() {
        // In this query, no sort will be used.
        return $this->skipOrderBy()->all();
    }
    
}

GlobalScope

You can set a scope to use for all queries used in the repository. If necessary, you can also ignore this global scope.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class AdminRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
    protected function globalScope() {
        return $this->where('is_admin', true);
    }
    
    public function getAdmins() {
        // In this query the declared global scope will be included.
        return $this->all();
    }
    
    public function getAll() {
        // In this query the declared global scope will not be included.
        return $this->skipGlobalScope()->all();
    }
}

Cache

The package comes with a powerful cache driver. The idea is that once the query is done, it will be cached. After the cache is done, it is possible to reduce the number of accesses to the database to zero. All caching is performed using the cache driver configured for the application. To use the driver, just use the trait that implements it.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
    protected function model() {
        return \App\User::class;
    }
}

Usage remains the same, with all cache management logic done automatically.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }
    
    public function index() {
        $users = $this->repository->getAllUsers();
        // if you call the same query later (even in other requests)
        // the query is already cached and you do not need to access the database again.
        $users = $this->repository->getAllUsers(); 
    }
    
}

Everytime the database data is changed, the cache is automatically cleaned to avoid outdated data. However, if it is necessary to clear the cache, it can be performed through the clearCache() method. You can also force access to the database and avoid cached data by using the skipCache() method.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
	protected function model() {
        return \App\User::class;
    }
    
    public function createUser($data) {
        // After inserting the data, the cache
        // is cleaned automatically.
        return $this->create($data);
    }
    
    public function addRules($user, $rules) {
        $user->rules()->attach($rules);
        $this->clearCache(); // Forcing cache cleaning.
    }
    
    public function getWithoutCache() {
        $this->skipCache()->all(); // Finding the data without using the cache.
    }
}

giordanolima/eloquent-repository 适用场景与选型建议

giordanolima/eloquent-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2017 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「repository」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 giordanolima/eloquent-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 giordanolima/eloquent-repository 我们能提供哪些服务?
定制开发 / 二次开发

基于 giordanolima/eloquent-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-24