michele-angioni / phalcon-repositories
一个简化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模型上执行许多有用的查询。这样,实现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
AbstractCollectionRepository
抽象类,类似于AbstractRepository
,包含一个模型包装器,用于在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 } }
方法列表
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 Repositories 遵循 PSR-1、PSR-2 和 PSR-4 PHP 编码标准,以及语义版本控制。
欢迎提交拉取请求。
许可协议
Phalcon Repositories 是免费软件,根据 MIT 许可协议分发。