milanpasic92/phalcon-repositories

这是一个简化在Phalcon中使用Repository模式的库。

v0.5.0 2018-06-07 09:20 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:52 UTC


README

License Latest Stable Version Latest Unstable Version Build Status

简介

Phalcon Repositories 允许您轻松地为您的Phalcon模型构建仓库,适用于 SQLMongo 驱动。

需要 PHP 7.1+ 和 Phalcon 3.2+。

安装

Phalcon Repositories 可以通过 Composer 安装,只需运行 composer require michele-angioni/phalcon-repositories

与 SQL 驱动器的使用

AbstractRepository 抽象类包含一个模型包装器,其中包含许多在 Phalcon 模型上执行的有用查询。这样,实现 Repository 模式就变得简单直接。

例如,假设我们有一个 MyApp\Models\Posts 模型。

创建 Posts 仓库的最简单方法是定义一个类如下

<?php

namespace MyApp\Repos;

use MicheleAngioni\PhalconRepositories\AbstractRepository;
use MyApp\Models\Posts;

class PostsRepository extends AbstractRepository
{
    protected $model;

    public function __construct(Posts $model)
    {
        $this->model = $model;
    }
}

现在,如果我们需要在 PostController 中使用 Post 仓库,例如,我们可以这样检索一个帖子

<?php

namespace MyApp\Controllers;

use MyApp\Repos\PostsRepository as PostsRepo;
use Phalcon\Mvc\Controller;
use MyApp\Models\Posts;

class PostsController extends Controller 
{
    
    public function showAction($idPost)
    {
        $postsRepo = new PostsRepo(new Posts());
        
        $post = $postsRepo->find($idPost);

        // Use the retrieved post
    }
}

我们还可以通过 Phalcon 依赖注入将我们的仓库绑定到容器中。我们只需要在启动文件中添加一个新的 postRepo 服务

$di->set('postsRepo', function () {
    return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts());
});

然后在控制器中使用它

<?php

namespace MyApp\Controllers;

use Phalcon\Mvc\Controller;

class PostsController extends Controller 
{
    
    public function showAction($idPost)
    {
        $postsRepo = $this->getDI()->getPostsRepo();
        
        $post = $postsRepo->find($idPost);

        // Use the retrieved Post
    }
}

与 MongoDB 的使用

AbstractRepository 类似,AbstractCollectionRepository 抽象类也包含一个模型包装器,其中包含许多在 Phalcon 集合上执行的有用查询。这样,实现 Repository 模式就变得简单直接。

例如,假设我们有一个 MyApp\Models\Posts 集合

<?php

namespace MyApp\Models;

use Phalcon\Mvc\MongoCollection;

class Posts extends MongoCollection
{
    use \MicheleAngioni\PhalconRepositories\MongoFix; // Fix for Phalcon 3.1.x with PHP 7.1

    [...]
}

创建 Posts 仓库的最简单方法是定义一个类如下

<?php namespace MyApp\Repos;

use MicheleAngioni\PhalconRepositories\AbstractCollectionRepository;
use MyApp\Models\Posts;

class PostsRepository extends AbstractCollectionRepository
{
    protected $model;

    public function __construct(Posts $model)
    {
        $this->model = $model;
    }
}

现在,如果我们需要在 PostController 中使用 Post 仓库,例如,我们可以这样检索一个帖子

<?php

namespace MyApp\Controllers;

use MyApp\Repos\PostsRepository as PostsRepo;
use Phalcon\Mvc\Controller;
use MyApp\Models\Posts;

class PostsController extends Controller
{

    public function showAction($idPost)
    {
        $postsRepo = new PostsRepo(new Posts());

        $post = $postsRepo->find($idPost);

        // Use the retrieved Post
    }
}

我们还可以通过 Phalcon 依赖注入将我们的仓库绑定到容器中。我们只需要在启动文件中添加一个新的 postRepo 服务

$di->set('postsRepo', function () {
    return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts());
});

然后在控制器中使用它

<?php

namespace MyApp\Controllers;

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{

    public function showAction($idPost)
    {
        $postsRepo = $this->getDI()->getPostsRepo();

        $post = $postsRepo->find($idPost);

        // Use the retrieved post
    }
}

方法列表

AbstractRepositoryAbstractCollectionRepository 自动赋予我们的仓库以下公共方法

  • all()
  • find($id)
  • findOrFail($id)
  • first()
  • firstOrFail()
  • firstBy(array $where = [])
  • firstOrFailBy(array $where = [])
  • getBy(array $where = [])
  • getByLimit(int $limit, array $where = [])
  • getByOrder(string $orderBy, array $where = [], string $order = 'desc', int $limit = 0)
  • getIn(string $whereInKey, array $whereIn = [], string $orderBy = null, string $order = 'desc', int $limit = 0)
  • getNotIn(string $whereNotInKey, array $whereNotIn = [], string $orderBy = null, string $order = 'desc', int $limit = 0)
  • getInAndWhereByPage(int $page = 1, int $limit = 10, string $whereInKey = null, array $whereIn = [], $where = [], $orderBy = null, string $order = 'desc')
  • getByPage(int $page = 1, int $limit = 10, array $where = [], string $orderBy = null, string $order = 'desc')
  • create(array $inputs = [])
  • updateById($id, array $inputs)
  • destroy($id)
  • destroyFirstBy(array $where)
  • count()
  • countBy(array $where = [])

AbstractRepository 还包含以下方法

  • getByGroupBy(string $groupBy, array $where = [], bool $addCounts = false)
  • truncate()

AbstractCollectionRepository 则允许通过以下方式执行聚合操作

  • getAggregate(array $match = [], array $project = [], array $group = [], int $limit = 0)

与 SQL 驱动器的 $where 参数

$where 参数允许与 SQL 驱动器一起使用各种运算符,而不仅仅是等于 = 运算符,甚至包括 LIKE 关键字。

以下格式受支持

  • 'key' => 'value'

    示例

    $where = ['username' => 'Richard']
  • 'key' => ['value', 'operator']

    示例

    $where = ['age' => [30, '=']]
    $where = ['age' => [30, '<']]
    $where = ['age' => [30, '>']]
    $where = ['username' => ['%Fey%', 'LIKE']]
  • ['key1%OR%key2'] => ['value', 'operator']

    示例

    `$where = ['username%OR%description' => ['%Feynman%', 'LIKE']]`

SQL 注入

AbstractRepositoryAbstractCollectionRepository 对所有 $id$where 子句使用绑定参数。在创建和更新查询中,$inputs 参数由 Phalcon 自动转义。

其他参数(如$whereInKey$whereIn$orderBy$order$limit等)的安全性由您负责。

测试

使用composer install安装依赖,然后运行vendor/bin/phpunit tests

贡献指南

Phalcon Repositories遵循PSR-1、PSR-2和PSR-4 PHP编码标准以及语义化版本控制。

欢迎提交拉取请求。

许可证

Phalcon Repositories是免费软件,根据MIT许可证条款分发。