mayconbordin/reloquent

Laravel 5 的存储库数据层

dev-master 2015-12-29 00:30 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:37:32 UTC


README

Build Status

详尽的 Laravel 5 存储库数据访问层。

另一个存储库包吗?

如果你在 Github 内部搜索,你可以找到至少十个用于 Laravel 4 和 5 的存储库模式实现。对于 Laravel 5,最好的是 prettus/l5-repositoryBosnadev/Repositories

这些包与这个包的区别在于更灵活的接口,它可以通过存储库执行更复杂的查询。还有很多测试。

安装

添加

"mayconbordin/reloquent": "dev-master"

到你的 composer.json 中。然后运行 composer installcomposer update

将服务提供者添加到 config/app.php 中的 providers 数组

'providers' => array(
    ...
    'Mayconbordin\Reloquent\Providers\ReloquentServiceProvider',
)

然后发布配置

php artisan vendor:publish

API & 使用

在以下部分,将给出存储库中所有接口方法的示例。

假设我们有一个具有以下列的 Post 类模型:idtitlecontentcategory_idauthor_id

findAllBy

// to find all posts that belong to a single category
$repository->findAllByCategoryId(1);

// fetch also the post tags and category
$repository->findAllByCategoryIdWith(1, ['tags', 'category']);

// we can also order the results by id in ascending order
$repository->findAllByCategoryIdOrderById(1);

// or descending
$repository->findAllByCategoryIdOrderByDescId(1);

// we could then limit the number of results returned to 15
$repository->findAllByCategoryIdOrderByIdLimit(1, 15);

// or even paginate the results, with 15 records per page
$repository->findAllByCategoryIdOrderByIdPaginated(1, 15);

// we can also get posts within a list of categories
$repository->findAllByInCategoryId([1, 2, 3, 4]);

// or filter the posts by category and author
$repository->findAllByCategoryIdAndAuthorId(1, 2);

// or by category or author
$repository->findAllByCategoryIdOrAuthorId(1, 2);

findBy

// find a single post by author id
$repository->findByAuthorId(1);

// or with author id AND category id
$repository->findByCategoryIdAndAuthorId(1, 2);

// or with author id OR category id
$repository->findByCategoryIdOrAuthorId(1, 2);

// fetch also the post tags and category objects
$repository->findByAuthorIdWith(1, ['tags', 'category']);

createupdatedelete

$attributes = [
  'title'       => 'Post Title',
  'content'     => 'Post content...',
  'category_id' => 1,
  'author_id'   => 1
];

// create a new post
$post = $repository->create($attributes);

// update the title of the post
$updatedPost = $repository->update(['title' => 'Update Post Title'], $post->id);

// and delete it
$repository->delete($updatedPost->id);

创建和删除具有其关系的模型

默认情况下,存储库不关心模型可能具有的关系,这意味着你不能将相关模型的实例作为属性值提供给 create 方法。你也不能在相关对象在问题对象之前被删除,如果数据库没有配置为删除子对象 (DELETE CASCADE),则操作将失败。

要让存储库了解关系,请向存储库实现添加 relations 属性,如下所示

class PostRepository extends BaseRepository
{
    protected $relations = ['category', 'author'];
    
    ...
}

现在你可以这样创建一个新的帖子

$attributes = [
  'title'    => 'Post Title',
  'content'  => 'Post content...',
  'category' => Category::find(1),
  'author'   => Author::find(1)
];

// create a new post
$post = $repository->create($attributes);

如果帖子也可能有标签,你可以这样创建带有标签的帖子

$attributes = [
  'title'    => 'Post Title',
  'content'  => 'Post content...',
  'category' => Category::find(1),
  'author'   => Author::find(1),
  'tags'     => [1, 2, 3, 4]
];

BelongsTo 关系使用 associate 方法保存,而 BelongsToMany 使用 attach 方法。当帖子被删除时,存储库将遍历定义的关系并将它们删除。对于 HasOneOrMany 实例的关系,使用 delete 方法,而对于 BelongsToMany,使用 detach 方法。

existsfindfindByField

// check if a post exists, returns a boolean
$repository->exists(1);

// find a post by id
$repository->find(1);

// bring also its relations
$repository->find(1, ['author', 'tags']);

// find a post by author
$repository->findByField('author_id', 1);

// find a post whose title starts with 'A'
$repository->findByField('title', 'A%', 'LIKE');

// and you can also bring the post's relations
$repository->findByField('title', 'A%', 'LIKE', ['author', 'tags']);

findWhere

findWhere 方法不如其他方法美观。第一个参数是一个包含 where 语句的关联数组。一个语句是一个包含字段值的数组,可选地包含运算符和语句。

$repository->findWhere([
    'title' => ['test'],        // WHERE title = 'test'
    'category_id' => ['!=', 2], // AND category_id != 2
    'author_id' => ['or', 1]    // OR author_id = 1
    'content' => ['or', 'like', '%test%']    // OR content LIKE %test%
]);

allfindAllByFieldfindAllWhere

// get all posts
$repository->all();

// get all posts ordered by title
$repository->all('title:asc');

// get all posts ordered by id and title
$repository->all(['id:asc', 'title:asc']);

// get all posts by author 
$repository->findAllByField('author_id', 1);

// get all posts not by author 
$repository->findAllByField('author_id', 1, '!=');

// get all posts by author ordered by title
$repository->findAllByField('author_id', 1, '=', 'title:asc');

// get 15 posts by author ordered by title
$repository->findAllByField('author_id', 1, '=', 'title:asc', ['author'], 15);

// same as the above query
$repository->findAllWhere(['author_id' => 1], 'title:desc', ['author'], 15);

paginatefindAllByFieldPaginatedfindAllWherePaginated

// get all posts
$repository->paginate();

// get 15 posts per page ordered by title
$repository->paginate(15, 'title:asc');

// get 15 posts per page posts ordered by id and title
$repository->paginate(15, ['id:asc', 'title:asc']);

// get 15 posts per page posts ordered by id and title with author
$repository->paginate(15, ['id:asc', 'title:asc'], ['author']);

// get posts by author 
$repository->findAllByFieldPaginated('author_id', 1);

// get posts not by author 
$repository->findAllByFieldPaginated('author_id', 1, '!=');

// get posts by author ordered by title
$repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc');

// get 15 posts per page by author ordered by title
$repository->findAllByFieldPaginated('author_id', 1, '=', 'title:asc', 15, ['author']);

// same as the above query
$repository->findAllWherePaginated(['author_id' => 1], 15, 'title:desc', ['author']);