two-bros/repository-pattern

1.02 2016-01-30 19:46 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:09:39 UTC


README

Composer

您可以通过运行以下命令通过Composer安装此包:composer require "two_bros/repository-pattern:~1.0"

或将包添加到您的composer.json

    {
        "require": {
            "two_bros/repository-pattern": "~1.0"
        }
    }

然后通过composer installcomposer update安装

基本用法

仓库模式的目的是将ORM的选择与应用程序分离,这样您就可以在不触及应用程序大多数代码的情况下更换数据层。这是迈向该目标的一个坚实的基础步骤,尽管还需要做一些事情才能使其完全与ORM分离。需要做的事情之一是不要从仓库中返回任何Eloquent特定的对象。

您需要让每个仓库继承TwoBros\RepositoryPattern\Repositories\LaravelAbstractRepository

此仓库将提供以下方法来支持您对大多数常规数据需求的基本需求。这意味着每个模型都应该有一个与之对应的仓库,以便您可以在应用程序和实际数据之间保持一层。随着特定领域内数据需求的演变,您会添加到您创建的特定领域仓库中。每个仓库还应该创建自己的接口,以确保任何特定领域功能都在合同下构建。这将确保如果需要更改ORM,则可以确切知道需要复制哪些功能。

create( array $attributes )

Will create a model from an array of attributes, and return the created model.

update( integer $id, array $attributes )

Will update a model using an id to locate and with an array of attributes, and return the updated model. 

destroy( integer $id )

Will destroy a model using an id to locate the model, and will return a 1 for the count of successfully deleted 
models.

all( array $columns = ['*'] )

Will return an Eloquent collection object with the columns requested, or all columns by default.

getById( integer $id, array $columns = ['*'], array $with = [] )

Will return a model with the columns requested and adding any relations requested. All columns are returned by default,
and no relations.

getByPage( integer $page = 1, integer $limit = 10, array $with = [] )

Will return a StdClass object that can be used in a Laravel Pagination object. It takes the page number, per page 
limit, and an array of relations that you want to include. The page defaults to 1 with 10 items per page, and no 
added relations. The return object will have an items array that includes an Eloquent collection object, and a 
totalItems property that will contain the count of total items in the database.

getFirstBy( string $key, string $value, array $with = [] )

Will return the first model from the database that contains the value for the selected column, and including any 
relations you decide to load. 

getManyBy( string $key, string $value, array $with = [] )

Will return an Eloquent collection object from the database that contains the value for the selected column, and including any 
relations you decide to load. 

has( string $relation, array $with = [] )

Will return an Eloquent collection object that has a specific relationship, and loading the array of relations provided.

make( array $with = [] )

Will return a model object with the array of relations loaded. This is more of an intermediary to an actual data return, 
and is used by any of the other methods that include loaded relations.

许可证

根据Apache许可证版本2.0(“许可证”)授权;除非适用法律要求或经书面同意,否则不得使用此文件,除非遵守许可证。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或经书面同意,否则在许可证下分发的软件按“现状”分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言规定权限和限制,请参阅许可证。