api-skeletons/zf-doctrine-criteria

此包已被弃用且不再维护。作者建议使用api-skeletons/doctrine-criteria包。

从数组参数创建Doctrine Criteria

1.0.2 2018-06-23 00:02 UTC

This package is auto-updated.

Last update: 2020-07-06 03:31:06 UTC


README

Build Status Coverage Gitter Patreon Total Downloads

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

安装

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

$ composer require api-skeletons/zf-doctrine-criteria

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

zf-component-installer

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

配置模块

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

注意,尚未支持AND和OR组合表达式。

使用

use Doctrine\Common\Util\ClassUtils;
use ZF\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 Collections当前不支持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']

包含

用于在字符串内部进行搜索。与“以...开头”和“以...结尾”搭配使用,包含匹配可以匹配任何部分中的字符串。

['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]]

按...排序

字段

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