claserre9/doctrine-generic-filter

提供一种简单的机制来过滤 Doctrine 查询

1.0.0 2023-10-10 00:23 UTC

This package is auto-updated.

Last update: 2024-09-10 14:29:53 UTC


README

本包提供了一个 GenericRepository 类,这是一个通用的实用类,旨在通过 Doctrine ORM 库简化数据库操作。它提供了检索分页结果、应用过滤和从数据库实体排序数据的方法。此 README 文件将指导您有效地使用此类。

目录

  • 安装
  • 使用方法
  • 方法
  • 示例
  • 贡献
  • 许可

安装

要使用 GenericRepository 类,您需要在 PHP 项目中设置 Doctrine ORM 库。

composer req claserre9/doctrine-generic-filter

使用方法

GenericRepository 类提供了检索数据库实体数据的方法,具有各种选项

  • getPaginatedResults:检索带有可选过滤和排序的分页结果。
  • getResults:检索不带分页的结果,但带有可选过滤和排序。

通过注入 EntityManagerInterface 创建 GenericRepository 类的实例

$entityManager = ...; // Instantiate your EntityManager
$repository = new GenericRepository($entityManager);

方法

getPaginatedResults

public function getPaginatedResults(
    string $entityClass,
    int $page = self::DEFAULT_PAGE,
    int $limit = self::DEFAULT_LIMIT,
    ?array $filters = []
): array
  • $entityClass(字符串):您要检索结果的实体的名称。
  • $page(整数):当前页码(默认为 1)。
  • $limit(整数):每页要获取的结果数量(默认为 10)。
  • $filters(数组或 null):要应用于查询的过滤器的数组。

返回一个包含分页结果以及分页元数据的数组。

getResults

public function getResults(
    string $entityClass,
    ?array $filters = []
): array

$entityClass(字符串):您要检索结果的实体的名称。$filters(数组或 null):要应用于查询的过滤器的数组。

返回一个包含不带分页的结果的数组。

示例

以下是使用 GenericRepository 类的一些示例

// Create an instance of GenericRepository (assuming $entityManager is already instantiated).
$repository = new GenericRepository($entityManager);

// Example 1: Get paginated results with filters and ordering.
$results = $repository->getPaginatedResults('Your\Entity\Class', 2, 15, ['column' => ['operator' => 'value']]);

// Example 2: Get results without pagination with filters and ordering.
$results = $repository->getResults('Your\Entity\Class', ['column' => ['operator' => 'value']]);

以下是支持的所有运算符列表

  • eq(等于)
  • neq(不等于)
  • gt(大于)
  • lt(小于)
  • gte(大于等于)
  • lte(小于等于)
  • in
  • notin(不在)
  • between
  • sort

一些具体的例子

/** Retrieve paginated results for the User entity with a filter
* on the gender field where it should be equal to 'Male'
*/

$filters = ['gender' => ["eq" => 'Male'],];
$results = $genericRepository->getPaginatedResults(User::class, 1, 10, $filters);
/** Retrieve paginated results for the User entity with a filter 
 * on the age field where it should be less than or equal to 20*
 */
$filters = ['age' => ["lte" => 20]];
$results = $genericRepository->getPaginatedResults(User::class, 1, 20, $filters);
/**
* Retrieve paginated results for the User entity with a filter 
 * on the age field where the age should be between 20 and 30
 */
$filters = ['age' => ["between" => ["start" => 20, "end" => 30]]];
$results = $genericRepository->getPaginatedResults(User::class, 1, 10, $filters);

我们还可以应用排序

/**
* Multiple filters and sort
 */


$filters = [
    'country' => ['eq' => 'China'],
    'gender' => ['eq' => 'Male'],
    'age' =>['sort' => 'DESC'],
    'id' => ['sort' => 'DESC'],
];

$results = $genericRepository->getPaginatedResults(User::class, 1, 10, $filters);

贡献

请自由地向此项目贡献,通过在 GitHub 仓库中打开问题或拉取请求。

许可

此项目使用 MIT 许可。

此 README 文件提供了对 GenericRepository 类、其用法以及如何在 PHP 项目中开始使用它的概述。如果您有任何问题或遇到问题,请参阅代码中提供的示例和文档,或寻求支持。

编码愉快!