adnanmula/criteria
Composer 安装命令:
composer require adnanmula/criteria
包简介
README 文档
README
A powerful PHP library for building dynamic database queries with a fluent interface, specifically optimized for PostgreSQL.
Table of Contents
Requirements
- PHP 8.4 or higher
- Doctrine DBAL 3.5 or higher
Installation
Install via Composer:
composer require adnanmula/criteria
Usage
Creating a Criteria Object
The Criteria class is the main entry point for building queries:
// Create a criteria object with filters, pagination and sorting $criteria = new Criteria( filters: new Filters( FilterType::AND, // How top-level filters are combined // Simple filter new Filter( new FilterField('status'), new StringArrayFilterValue('active', 'pending', 'review'), FilterOperator::IN, ), // Filter composed of other filters new CompositeFilter( FilterType::OR, // How each expression of this composite filter is combined new Filter( new FilterField('id'), new StringFilterValue('abc123'), FilterOperator::EQUAL, ), new Filter( new FilterField('id'), new StringFilterValue('asdasd'), FilterOperator::EQUAL, ), ), // Composite filters can contain other composite filters (example: (id = 'abc123') OR (amount <= 3 AND json_field @> '["value"]') new CompositeFilter( FilterType::OR, new Filter( new FilterField('id'), new StringFilterValue('abc123'), FilterOperator::EQUAL, ), new CompositeFilter( FilterType::AND, new Filter( new FilterField('amount'), new IntFilterValue(3), FilterOperator::LESS_OR_EQUAL, ), new Filter( new FilterField('json_field'), new ArrayElementFilterValue('value'), FilterOperator::IN_ARRAY, ), ), ), // You can add any number of filters or composite filters ), offset: 10, limit: 20, sorting: new Sorting( new Order( new FilterField('name'), OrderType::ASC, ), new Order( new FilterField('created_at'), OrderType::DESC, ), ), ); // Helper methods // Copy a criteria adding any number of filters $originalCriteria = new Criteria(); $newCriteria = new Criteria()->with( new Filter(new FilterField('otherField'), new NullFilterValue(), FilterOperator::IS_NULL), new CompositeFilter( FilterType::OR, new Filter(new FilterField('domainId'), new IntFilterValue(3), FilterOperator::EQUAL), new Filter(new FilterField('random_string_or_null'), new StringFilterValue('imnotrandom'), FilterOperator::EQUAL), ), ); // Copy a criteria removing pagination $newCriteria = $criteria->withoutPagination(); // Copy a criteria removing pagination and sorting $newCriteria = $criteria->withoutPaginationAndSorting(); // Copy a criteria removing filters $newCriteria = $criteria->withoutFilters();
Using with Doctrine DBAL
The library integrates with Doctrine DBAL's QueryBuilder:
// Get a query builder from your Doctrine DBAL connection $builder = $this->connection->createQueryBuilder(); // Build your base query $query = $builder->select('a.fields') ->from('table', 'a'); // Apply criteria to the query builder (new DbalCriteriaAdapter($builder))->execute($criteria); // Execute the query $result = $query->executeQuery()->fetchAllAssociative();
Filter Operators
The library supports various filter operators:
- Comparison:
EQUAL,NOT_EQUAL,GREATER,GREATER_OR_EQUAL,LESS,LESS_OR_EQUAL - Text search:
CONTAINS,NOT_CONTAINS,CONTAINS_INSENSITIVE,NOT_CONTAINS_INSENSITIVE - Collection:
IN,NOT_IN - Null checks:
IS_NULL,IS_NOT_NULL - JSON array operations:
IN_ARRAY,NOT_IN_ARRAY
Filter Fields
FilterField: Standard field for most column typesJsonKeyFilterField: For accessing JSON/JSONB fields with specific keys
Filter Values
Value types for different data types:
StringFilterValue: For string valuesIntFilterValue: For integer valuesStringArrayFilterValue: For arrays of strings (used with IN and NOT_IN operators)IntArrayFilterValue: For arrays of integers (used with IN and NOT_IN operators)ArrayElementFilterValue: For JSON array operationsNullFilterValue: For NULL checks
License
This project is licensed under the MIT License - see the LICENSE file for details.
adnanmula/criteria 适用场景与选型建议
adnanmula/criteria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 389 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 adnanmula/criteria 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adnanmula/criteria 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 389
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-12