victorhramos / phalcon-repo
这是一个简化Phalcon中Repository模式使用的库。
Requires
- php: ^7.1
- ext-phalcon: ^3.2
- phalcon/incubator: ^3.2
Requires (Dev)
- phpunit/phpunit: ^5.7
README
简介
Phalcon Repositories 允许您轻松为 Phalcon 模型构建仓库,适用于 SQL 和 Mongo 驱动程序。
需要 PHP 7.1+ 和 Phalcon 3.2+。
安装
Phalcon Repositories 可以通过 Composer 安装,只需运行 composer require michele-angioni/phalcon-repositories
。
与 SQL 驱动程序一起使用
AbstractRepository
抽象类由一个模型包装器组成,包含许多在 Phalcon 模型上执行的有用查询。这样实现仓库模式就变得简单直接。
例如,假设我们有一个 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 仓库,例如,我们可以这样检索一个 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 一起使用
AbstractCollectionRepository
抽象类,与 AbstractRepository
类似,由一个模型包装器组成,包含许多在 Phalcon 集合上执行的有用查询。这样实现仓库模式也变得简单直接。
例如,假设我们有一个 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 仓库,例如,我们可以这样检索一个 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 } }
方法列表
AbstractRepository
和 AbstractCollectionRepository
自动赋予我们的仓库以下公开方法
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 注入
AbstractRepository
和 AbstractCollectionRepository
使用绑定参数为所有 $id
和 $where
子句。在创建和更新查询中,$inputs
参数由 Phalcon 自动转义。
其他参数(如$whereInKey
、$whereIn
、$orderBy
、$order
、$limit
等)的安全性由您负责。
测试
使用composer install
安装依赖项,然后运行vendor/bin/phpunit tests
。
贡献指南
Phalcon 仓库遵循 PSR-1、PSR-2 和 PSR-4 PHP 编码标准以及语义化版本控制。
欢迎提交拉取请求。
许可证
Phalcon 仓库是免费软件,根据 MIT 许可证的条款进行分发。