milanpasic92/phalcon-repositories
Composer 安装命令:
composer require milanpasic92/phalcon-repositories
包简介
A library which simplifies the use of the Repository pattern in Phalcon.
关键字:
README 文档
README
Introduction
Phalcon Repositories lets you easily build repositories for your Phalcon models, for both SQL and Mongo drivers.
PHP 7.1+ and Phalcon 3.2+ are required.
Installation
Phalcon Repositories can be installed through Composer, just run composer require michele-angioni/phalcon-repositories.
Usage with SQL Drivers
The abstract class AbstractRepository consists of a model wrapper with numerous useful queries to be performed over the Phalcon models.
This way implementing the repository pattern becomes straightforward.
As an example let's say we have a MyApp\Models\Posts model.
The easiest way to create a Posts repository is to define a class as such
<?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; } }
Suppose now we need the Post repository in our PostController. For example we can retrieve a Post this way
<?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 } }
We could also bind out repository to the container through the Phalcon dependency injection.
We just need to add a new postRepo service in our bootstrap file
$di->set('postsRepo', function () { return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts()); });
and than use it in the controller
<?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 } }
Usage with MongoDB
The abstract class AbstractCollectionRepository, similary to AbstractRepository, consists of a model wrapper with numerous useful queries to be performed over the Phalcon collections.
This way implementing the repository pattern becomes straightforward.
As an example let's say we have a MyApp\Models\Posts collection
<?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 [...] }
The easiest way to create a Posts repository is to define a class as such
<?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; } }
Suppose now we need the Post repository in our PostController. For example we can retrieve a Post this way
<?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 } }
We could also bind out repository to the container through the Phalcon dependency injection.
We just need to add a new postRepo service in our bootstrap file
$di->set('postsRepo', function () { return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts()); });
and than use it in the controller
<?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 } }
Method list
The AbstractRepository and AbstractCollectionRepository empower automatically our repositories of the following public methods:
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 = [])
The AbstractRepository contains also the methods:
getByGroupBy(string $groupBy, array $where = [], bool $addCounts = false)truncate()
while the AbstractCollectionRepository allows for aggregations through:
getAggregate(array $match = [], array $project = [], array $group = [], int $limit = 0)
The $where parameter with SQL drivers
The $where parameter allows the use of various operators with the SQL driver, other than the equals =, even the LIKE keyword.
The following formats are supported:
-
'key' => 'value'Examples:
$where = ['username' => 'Richard']
-
'key' => ['value', 'operator']Examples:
$where = ['age' => [30, '=']] $where = ['age' => [30, '<']] $where = ['age' => [30, '>']] $where = ['username' => ['%Fey%', 'LIKE']]
-
['key1%OR%key2'] => ['value', 'operator']Examples:
`$where = ['username%OR%description' => ['%Feynman%', 'LIKE']]`
SQL Injection
The AbstractRepository and AbstractCollectionRepository use bind parameters for all $id and $where clauses.
$inputs parameters in create and update queries are automatically escaped by Phalcon.
The security of the other parameters ($whereInKey, $whereIn, $orderBy, $order, $limit etc.) is up to you.
Testing
Install dependencies with composer install and then run vendor/bin/phpunit tests.
Contribution guidelines
Phalcon Repositories follows PSR-1, PSR-2 and PSR-4 PHP coding standards, and semantic versioning.
Pull requests are welcome.
License
Phalcon Repositories is free software distributed under the terms of the MIT license.
milanpasic92/phalcon-repositories 适用场景与选型建议
milanpasic92/phalcon-repositories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 903 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repositories」 「phalcon」 「repo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 milanpasic92/phalcon-repositories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 milanpasic92/phalcon-repositories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 milanpasic92/phalcon-repositories 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library which provide you with simple authentication.
StyleCI Git Is A Git Repository Manager
A Laravel package for the Repository Design Pattern.
Creating a Laravel Repositories
Simple, safe and flexible implementation of handling SQL entities and repositories as well as multi-table SQL queries while staying lightweight and straightforward.
Symfony integration of squirrelphp/entities - automatic integration of generated repositories for existing entities.
统计信息
- 总下载量: 903
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-31