api-skeletons/doctrine-criteria

从数组参数生成 Doctrine Criteria

2.0.3 2020-10-05 21:37 UTC

This package is auto-updated.

Last update: 2024-09-06 05:48:20 UTC


README

Build Status Coverage Gitter Patreon Total Downloads

此库从数组参数构建 Criteria 对象,用于过滤集合。

安装

此模块的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org

$ composer require api-skeletons/doctrine-criteria

安装完成后,将 ApiSkeletons\Doctrine\Criteria 添加到 config/application.config.phpconfig/modules.config.php 中的模块列表中。

laminas-component-installer

如果您使用 laminas-component-installer,则该插件将为您将 doctrine-criteria 作为模块安装。

配置模块

config/apiskeletons-doctrine-criteria.global.php.dist 复制到 config/autoload/apiskeletons-doctrine-criteria.global.php 并编辑您想要启用的别名列表。默认情况下,所有支持的表达式都启用。

注意,目前尚不支持 AND 和 OR 组合表达式。

用法

use Doctrine\Common\Util\ClassUtils;
use ApiSkeletons\Doctrine\Criteria\Builder as CriteriaBuilder;

$filterArray = [
    [
        'type' => 'eq',
        'field' => 'name',
        'value' => 'Grateful Dead',
    ],
    [
        'type' => 'beginswith',
        'field' => 'state',
        'value' => 'UT',
    ],
];

$orderByArray = [
    [
        'type' => 'field',
        'field' => 'venue',
        'direction' => 'asc',
    ]
];

$criteriaBuilder = $container->get(CriteriaBuilder::class);
$entityClassName = ClassUtils::getRealClass(get_class($collection->first()));
$metadata = $objectManager->getClassMetadata($entityClassName);
$criteria = $criteriaBuilder->create($metadata, $filterArray, $orderByArray);

$filteredCollection = $collection->matching($criteria);

过滤器

过滤器不是简单的键/值对。过滤器是键的数组,每个过滤器定义是一个数组,数组值因过滤器类型而异。

每个过滤器定义至少需要有一个 'type'。类型引用配置键,例如 'eq'、'neq'、'contains'。

每个过滤器定义至少需要有一个 'field'。这是目标实体上的字段名称。

每个过滤器定义可以指定 'where',其值为 'and' 或 'or'。

日期字段的格式

当日期字段涉及过滤器时,您可以使用 PHP 日期格式选项指定日期的格式。默认日期格式是 ISO 8601 Y-m-d\TH:i:sP 如果您有一个仅为 Y-m-d 的日期字段,则将格式添加到过滤器中。有关完整的日期格式选项,请参阅 DateTime::createFromFormat

[
    'format' => 'Y-m-d',
    'value' => '2014-02-04',
]

包含的过滤器类型

等于

Doctrine 集合目前不支持 DateTime 的 Equals 比较。通过 equals 过滤器发送的任何 DateTime 值都会始终返回不相等。这是 doctrine/collections 的不足,而非此模块的不足。其他比较运算符应按预期工作。

['type' => 'eq', 'field' => 'fieldName', 'value' => 'matchValue']

不等于

['type' => 'neq', 'field' => 'fieldName', 'value' => 'matchValue']

小于

['type' => 'lt', 'field' => 'fieldName', 'value' => 'matchValue']

小于或等于

['type' => 'lte', 'field' => 'fieldName', 'value' => 'matchValue']

大于

['type' => 'gt', 'field' => 'fieldName', 'value' => 'matchValue']

大于或等于

['type' => 'gte', 'field' => 'fieldName', 'value' => 'matchValue']

包含

用于在字符串内部搜索。与 Starts With 和 Ends With 互补,contains 匹配值中的任何部分的字符串。

['type' => 'contains', 'field' => 'fieldName', 'value' => 'matchValue']

以...开头

['type' => 'startswith', 'field' => 'fieldName', 'value' => 'matchValue']

以...结尾

['type' => 'endswith', 'field' => 'fieldName', 'value' => 'matchValue']

成员

用于在数组字段内部搜索以匹配 matchValue 到数组元素。

['type' => 'memeberof', 'field' => 'fieldName', 'value' => 'matchValue']

在...中

注意:In 和 NotIn 过滤器中的日期不按日期处理。建议您使用其他过滤器代替这些过滤器用于日期数据类型。

['type' => 'in', 'field' => 'fieldName', 'values' => [1, 2, 3]]

不在...中

注意:In 和 NotIn 过滤器中的日期不按日期处理。建议您使用其他过滤器代替这些过滤器用于日期数据类型。

['type' => 'notin', 'field' => 'fieldName', 'values' => [1, 2, 3]]

为空

用于确定字段是否为空。没有相应的 IsNotNull。

['type' => 'isnull', 'field' => 'fieldName']

排序

字段

['type' => 'field', 'field' => 'fieldName', 'direction' => 'desc']