定制 doctorbeat/eloquent-repository 二次开发

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

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

doctorbeat/eloquent-repository

最新稳定版本:v1.0.2

Composer 安装命令:

composer require doctorbeat/eloquent-repository

包简介

A repository for eloquent models. Using this makes the model appear to have a sigle responsibility

README 文档

README

A repository for eloquent models. Using this makes the model appear to have a single responsibility and thus makes them unit-testable in isolation

Installation

Via composer:

require: "doctorbeat/eloquent-repository": "*"

Why a respository?

Eloquent is great for its simplicity but this simplicity makes that Eloquent models breaks the 'single responsibility" principle. An Eloquent model is both a entity (property bag) AND dealing with database interaction (select, insert, update, delete). And this makes that you can not mock your models in isolation. You need the entity functionality but want to mock the database interaction.

This problem is further shown in the fact that Eloquent models use static functions like find(), all() and where().

In comes the Eloquent Repository which make you able to use a model as an entity/property bag moves all database interaction methods to the repository. The repository comes with an interface which can be mocked.

Methods and api

These methods are available on the repository and have an api equal to the corresponding Eloquent method:

  • save
  • delete
  • push
  • touch
  • all
  • find
  • where*
  • all other static methods

Relations

To get relations from the repository use these methods:

   [Model:]           [Repository:]                             [result]
-  $model->parent     $repo->getAttribute($model, 'parent');    the parent model
-  $model->children   $repo->getAttribute($model, 'children');  the child models as a collection
-  $model->children() $repo->children($model);                  the child models as a relation
-  $model->children()->save($child)
                      $repo->children($model, $child);          add the child to the children of the model
-  $model->children()->saveMany($list)
                      $repo->children($model, $list);           add an array of new children to the children of the model

These examples assume that you have defined parent and children as relations on the model!

Usage

        $repo = new EloquentRepository('App\\Person');
        
        $ent = new Person();
        $ent->name = 'xyz';
        $repo->save($ent);
        $repo->delete($ent);
        
        $all = $repo->all();
        $person4 = $repo->find(4);
        $personXyz = $repo->whereName('xyz')->get();

统计信息

  • 总下载量: 34
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-25