garak/orm-criteria
Composer 安装命令:
composer require garak/orm-criteria
包简介
Apply filters to Doctrine Query Builder
README 文档
README
This library is meant to ease the filtering with Doctrine repositories. It's an ideal companion for PUGX FilterBundle.
Setup
Run composer require garak/orm-criteria.
If you want to leverage the profiler info, you need to add the bundle to your kernel. You won't need to enable the bundle in other environments, its features will work anyway.
<?php return [ // your other bundles... Garak\OrmCriteria\GarakOrmCriteriaBundle::class => ['dev' => true], ];
Usage
The basic idea of this library is to apply the Open/Closed principle (the "O" in SOLID), to avoid being forced to change your code every time you need to apply a new filter.
Tip
for the concept of "filter", refer to the FilterBundle mentioned above
You have two classes available: AbstractCriterion and Filterer.
Inject the latter into your repositories, then you can start creating
your criteria.
Let's suppose you have a UserRepository, and you want to filter your users by
the following fields: "username", "enabled" (yes/no), and "country".
Let's create the following classes:
<?php namespace YourNamespace\Repository\Criteria\User; use Doctrine\ORM\QueryBuilder; use Garak\OrmCriteria\AbstractCriterion; use YourDomain\Entity\User; final class CountryUserCriterion extends AbstractCriterion { protected static string $className = User::class; protected static string $field = 'country'; }
<?php namespace YourNamespace\Repository\Criteria\User; use Doctrine\ORM\QueryBuilder; use Garak\OrmCriteria\AbstractCriterion; use YourDomain\Entity\User; final class EnabledUserCriterion extends AbstractCriterion { protected static string $className = User::class; protected static string $field = 'enabled'; }
<?php namespace YourNamespace\Repository\Criteria\User; use Doctrine\ORM\QueryBuilder; use Garak\OrmCriteria\AbstractCriterion; use YourDomain\Entity\User; final class UsernameUserCriterion extends AbstractCriterion { protected static string $className = User::class; protected static string $field = 'username'; protected static string $compare = self::LIKE; }
Then configure the services:
services: _defaults: autowire: true autoconfigure: true # feel free to use the tag name you prefer _instanceof: Garak\OrmCriteria\AbstractCriterion: tags: ['garak.criterion'] # if you changed the tag name above, be sure that you use the same name here Garak\OrmCriteria\Filterer: bind: $criteria: !tagged_iterator garak.criterion
Now, let's use it in your repository:
<?php namespace YourNamespace\Repository\UserRepository; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\QueryBuilder; use Garak\OrmCriteria\Filterer; use YourDomain\Entity\User; readonly class UserRepository { public function __construct( private EntityManagerInterface $manager, private Filterer $filterer, ) { }; public function listUsers(array $filters): array { $builder = $this->manager ->createQueryBuilder() ->from(User::class, 'u') ->select('u') ; $this->filterer->filter(User::class, $filters); return $builder->getQuery->execute(); } }
Advanced Usage
You can pass your sorting options along wit the filters, like in this example:
$filters['_sort']['field'] = 'username';
$filters['_sort']['direction'] = 'DESC';
If your criterion needs something more sophisticated than the basic operators,
you can define a filter method and add your logic. Example:
<?php namespace YourNamespace\Repository\Criteria\User; use Doctrine\ORM\QueryBuilder; use Garak\OrmCriteria\AbstractCriterion; use YourDomain\Entity\User; final class UnchartedUserCriterion extends AbstractCriterion { protected static string $className = User::class; protected static string $field = 'map'; protected static function filter(QueryBuilder $builder, string $value, string $alias): void { $builder ->andWhere($alias.'.coordinates.latitudine is null') ->andWhere($alias.'.coordinated.longitudine is null') ; } }
You can use different kinds of comparison operators, see the constants defined in
the Filterer class.
By default, the library expects to find a database field matching the name of the
filtered field: for example, in the code above, the filtered field username expects
a db field u.username.
If it's not the case, you can define a static property $dbField in your criterion class.
garak/orm-criteria 适用场景与选型建议
garak/orm-criteria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.66k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「doctrine」 「filter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 garak/orm-criteria 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 garak/orm-criteria 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 garak/orm-criteria 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Symfony DataGridBundle
Make pimcore migration simple
Data provider for yii2
This Laravel Nova package allows you to detach filters from the filter dropdown
Laravel filter elequent
统计信息
- 总下载量: 2.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2024-05-06