saf / doctrine-orm-searchable-repository

此包已被废弃,不再维护。未建议替代包。

使 Doctrine 的 EntityRepository 可搜索

0.2.2 2019-01-29 14:36 UTC

This package is auto-updated.

Last update: 2021-03-22 13:45:52 UTC


README

此库扩展了 EntityRepository 以添加具有许多过滤器的搜索功能。

$filters = [
    'name'        => ['neq' => 'My name'],
    'author.name' => ['in' => ['Foo', 'Bar']],
    'rating'      => ['gte' => 4],
];

$orders = [
    'author.name' => 'ASC',
    'name'        => 'DESC',
];

$entities = $repository->search($filters, $orders);

甚至可以搜索多个列上的值

$filters = [
    'name' => [
        'field'     => ['firstName', 'lastName],
        'condition' => 'like',
        'value'     => '%Lewis%',
    ],
];

$orders = [
    'author.name' => 'ASC',
    'name'        => 'DESC',
];

$entities = $repository->search($filters, $orders);

这将导致以下条件

firstName like '%Lewis%' OR lastName like '%Lewis%'

如何使用

使用 composer 安装

composer require saf/doctrine-orm-searchable-repository

然后只需让您的仓库类继承 SAF\SearchableRepository\SearchableRepository

use SAF\SearchableRepository\SearchableRepository;

class MyRepository extends  SearchableRepository
{
    // ...
}

然后您可以在您的仓库上访问 search 方法。

过滤器

此库使用过滤器类型来处理如何应用过滤器条件和排序。它包含一个泛型类型,这是默认的,但您可以添加新的过滤器类型。

// add a new type handler to your search with the setType function
// the first parameter correspond to the doctrine type
// the second parameter is the filter type (that must implements \SAF\SearchableRepository\SearchableRepository\Types\TypeInterface interface)
$repository->setType('string', new MyStringFilterType());

泛型类型

过滤器 支持值 行为
eq mixed 过滤与给定值匹配的元素
neq mixed 过滤不匹配给定值的元素
lt mixed 过滤值小于给定值的元素
lte mixed 过滤值小于或等于给定值的元素
gt mixed 过滤值大于给定值的元素
gte mixed 过滤值大于或等于给定值的元素
like string 使用 LIKE 语句过滤元素
not_like string 使用 NOT LIKE 语句过滤元素
null boolean true: 过滤具有 null 值的元素 false : 过滤具有非 null 值的元素
not_null boolean true: 过滤具有非 null 值的元素 false: 过滤具有 null 值的元素
in array 过滤具有给定数组中值的元素
not_in array 过滤具有给定数组中值的元素