rikbruil/doctrine-specification
Composer 安装命令:
composer require rikbruil/doctrine-specification
包简介
Doctrine Specification pattern for building queries dynamically and with re-usable classes for composition.
README 文档
README
Doctrine Specification pattern for building queries dynamically and with re-usable classes for composition.
This library started out as an adaptation of Benjamin Eberlei's blog post. I was also inspired by the Happyr Doctrine-Specification code, however this library has some small differences. The main one is that SpecificationRepository->match() does not return the results directly, but returns the query object.
Since I like Doctrine's Paginator object, I wanted to be able to use that in combination with the Specification pattern.
Note: In versions prior to 1.2 it was required to extend the SpecificationRepository class. This is no longer needed since we provide a SpecificationRepositoryTrait that you can use instead. The class is still provided for backwards compatibility reasons. There is also the SpecificationAwareInterface that you can use if you need it.
Usage
Install the latest version with composer require rikbruil/doctrine-specification
// Not using the lib // Note: Advertisement repository is an instance of the Doctrine default repository class $qb = $this->em->getRepository('Advertisement') ->createQueryBuilder('r'); return $qb->where('r.ended = 0') ->andWhere( $qb->expr()->orX( 'r.endDate < :now', $qb->expr()->andX( 'r.endDate IS NULL', 'r.startDate < :timeLimit' ) ) ) ->setParameter('now', new \DateTime()) ->setParameter('timeLimit', new \DateTime('-4weeks')) ->getQuery() ->getResult();
use Rb\Specification\Doctrine\Condition\Equals; use Rb\Specification\Doctrine\Condition\IsNull; use Rb\Specification\Doctrine\Condition\LessThan; use Rb\Specification\Doctrine\Logic\AndX; use Rb\Specification\Doctrine\Logic\OrX; use Rb\Specification\Doctrine\Specification; // Using the lib $spec = new Specification([ new Equals('ended', 0), new OrX( new LessThan('endDate', new \DateTime()), new AndX( new IsNull('endDate'), new LessThan('startDate', new \DateTime('-4weeks')) ) ) ]); // Note: Advertisement repository is an instance that uses the SpecificationRepositoryTrait return $this->em->getRepository('Advertisement')->match($spec)->execute();
Composition
A bonus of this pattern is composition, which makes specifications very reusable:
use Entity\Advertisement; class ExpiredAds extends Specification { public function __construct() { $specs = [ new Equals('ended', 0), new OrX( new LessThan('endDate', new \DateTime()), new AndX( new IsNull('endDate'), new LessThan('startDate', new \DateTime('-4weeks')) ) ) ]; parent::__construct($specs); } public function isSatisfiedBy($value) { return $value === Advertisement::class; } } use Entity\User; class AdsByUser extends Specification { public function __construct(User $user) { $specs = [ new Select('u'), new Join('user', 'u'), new Equals('id', $user->getId(), 'u'), ]; parent::__construct($specs); } public function isSatisfiedBy($value) { return $value == Advertisement::class && parent::isSatisfiedBy($value); } } class SomeService { /** * Fetch Adverts that we should close but only for a specific company */ public function myQuery(User $user) { $spec = new Specification([ new ExpiredAds(), new AdsByUser($user), ]); return $this->em->getRepository('Advertisement')->match($spec)->execute(); } /** * Fetch adverts paginated by Doctrine Paginator with joins intact. * A paginator can be iterated over like a normal array or Doctrine Collection */ public function myPaginatedQuery(User $user, $page = 1, $size = 10) { $spec = new Specification([ new ExpiredAds(), new AdsByUser($user), ]); $query = $this->em->getRepository('Advertisement')->match($spec); $query->setFirstResult(($page - 1) * $size)) ->setMaxResults($size); return new Paginator($query); } }
Requirements
Doctrine-Specification requires:
- PHP 5.5+
- Doctrine 2.2
License
Doctrine-Specification is licensed under the MIT License - see the LICENSE file for details
Acknowledgements
This library is heavily inspired by Benjamin Eberlei's blog post and Happyr's Doctrine-Specification library.
rikbruil/doctrine-specification 适用场景与选型建议
rikbruil/doctrine-specification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 256.25k 次下载、GitHub Stars 达 50, 最近一次更新时间为 2015 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repository」 「doctrine」 「specification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rikbruil/doctrine-specification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rikbruil/doctrine-specification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rikbruil/doctrine-specification 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
repository php library
Specification support for Doctrine
Laravel 5 - Repositories to the database layer
Specifications like JSON schemas and other things for mosparo
Make pimcore migration simple
统计信息
- 总下载量: 256.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 52
- 点击次数: 27
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-07