ifedko/php-doctrine-dbal-pagination

使用 doctrine dbal 的 PHP 分页构建器

3.0.3 2023-07-13 14:02 UTC

This package is auto-updated.

Last update: 2024-09-04 04:40:22 UTC


README

目标

每个人在需要通过自定义过滤器获取项目列表、按自定义参数排序这些项目以及分页时都会面临这样的任务。这个库可以帮助你做到这一点。

用法

入门

示例使用表 usersidis_activenamecreated_at)。

首先创建你的构建器。在那里需要定义基本查询

<?php

namespace Foo\Bar;

use Ifedko\DoctrineDbalPagination\ListBuilder;

class MyListBuilder extends ListBuilder
{
	protected function baseQuery()
	{
	    $builder = $this->getQueryBuilder();
        $builder
            ->select(['u.id, u.name, u.created_at'])
            ->from('users', 'u')
            ->where('u.is_active IS TRUE');

        return $queryBuilder;
	}
}

然后使用构建器

<?php

namespace Foo\Bar;

use Ifedko\DoctrineDbalPagination\ListPagination;
use Foo\Bar\MyListBuilder;

$dbConnection = \Doctrine\DBAL\DriverManager::getConnection(['url' => 'mysql://dbuser:dbpasswd@127.0.0.1/dbname']);
$limit = 10;
$offset = 20;
$parameters = [];

$listBuilder = new MyListBuilder(
	$dbConnection
);
$listBuilder->configure($parameters);

$listPagination = new ListPagination($listBuilder);
$listPage = $listPagination->get($limit, $offset);

where

$limit 是每页项目数量(默认为 20

$offset 是起始行号(默认为 0

过滤

configureFilters 方法添加到你的 MyListBuilder 类中,以使用可自定义的过滤。

此示例解释了如何通过某些字段开启过滤器:idnamecreated_at

...

protected function configureFilters($parameters)
{
    $mapAvailableFilterByParameter = [
        'id' => new EqualFilter('id', \PDO::PARAM_INT),
        'name' => new EqualFilter('name', \PDO::PARAM_STR),
        'created_at_from' => new GreaterThanOrEqualFilter('user.created_at'),
        'created_at_to' => new LessThanOrEqualFilter('user.created_at')
    ];

    /* @var $filter FilterInterface */
    foreach ($mapAvailableFilterByParameter as $parameterName => $filter) {
        if (isset($parameters[$parameterName])) {
            $filter->bindValues($parameters[$parameterName]);
            $this->filters[] = $filter;
        }
    }

    return $this;
}
...

where

parameters 是一个数组,可以包含用于过滤的参数(参见使用构建器以上部分)

EqualFilterGreaterThanOrEqualFilterLessThanOrEqualFilter 是一些可用的过滤器(以下将介绍所有过滤器)

例如,

$parameters = [
    'id' => 1,
    'title' => 'name'
];

排序

configureSorting 方法添加到你的 MyListBuilder 类中

...
use Ifedko\DoctrineDbalPagination\Sorting\ByColumn;

...

protected function configureSorting($parameters)
{
    $this->sortUsing(new ByColumn('id', 'user_id'), $parameters);
    $this->sortUsing(new ByColumn('name', 'name'), $parameters);
    $this->sortUsing(new ByColumn('from', 'user.created_at'), $parameters);
    $this->sortUsing(new ByColumn('to', 'user.created_at'), $parameters);

    return $this;
}
...

where

parameters 是一个数组,可以包含用于排序的参数(参见使用构建器以上部分)

例如,

$parameters = [
    'sortBy' => 'name',
    'orderBy' => 'DESC'
];

过滤器

  • 日期范围过滤器
  • 等于过滤器
  • 大于等于过滤器
  • 小于等于过滤器
  • 类似过滤器
  • 多等于过滤器
  • 多类似过滤器

多类似过滤器

此过滤器支持复杂的搜索查询,如由空格分隔的子字符串。它通过提供更多的搜索词来缩小结果。还可以进行谈判,如 '-word'。

示例

搜索: bla 结果(3)

搜索: bla fi 结果(2)

搜索: bla fi -final 结果(1)

选项

可以定义选项

  • 运算符 - 比较运算符,例如 ILIKE
  • matchFromStart - 从字符串开始匹配的列数组,例如 col like 'substring%'