lampager/lampager-idiorm

为 Idiorm 和 Paris 提供快速分页

v0.2.3 2021-06-24 11:58 UTC

This package is auto-updated.

Last update: 2024-09-15 08:49:59 UTC


README

lampager-idiorm

Build Status Coverage Status Scrutinizer Code Quality

Lampager for Idiorm and Paris

不使用 OFFSET 的快速分页

需求

安装

composer require lampager/lampager-idiorm

基本用法

您可以使用全局函数 lampager() 将 ORM 实例包装起来,使其可链式调用。

$cursor = [
    'id' => 3,
    'created_at' => '2017-01-10 00:00:00',
    'updated_at' => '2017-01-20 00:00:00',
];

$result = lampager(ORM::for_table('posts')->where_equal('user_id', 1))
    ->forward()
    ->limit(5)
    ->order_by_desc('updated_at') // ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
    ->order_by_desc('created_at')
    ->order_by_desc('id')
    ->seekable()
    ->paginate($cursor)
    ->to_json(JSON_PRETTY_PRINT);

它将运行优化的查询。

SELECT * FROM (

    SELECT * FROM `posts`
    WHERE `user_id` = 1
    AND (
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` > 3
        OR
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` > '2017-01-10 00:00:00'
        OR
        `updated_at` > '2017-01-20 00:00:00'
    )
    ORDER BY `updated_at` ASC, `created_at` ASC, `id` ASC
    LIMIT 1

) `temporary_table`

UNION ALL

SELECT * FROM (

    SELECT * FROM `posts`
    WHERE `user_id` = 1
    AND (
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` <= 3
        OR
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` < '2017-01-10 00:00:00'
        OR
        `updated_at` < '2017-01-20 00:00:00'
    )
    ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
    LIMIT 6

) `temporary_table`

然后您将得到

{
  "records": [
    {
      "id": 3,
      "user_id": 1,
      "text": "foo",
      "created_at": "2017-01-10 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 5,
      "user_id": 1,
      "text": "bar",
      "created_at": "2017-01-05 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 4,
      "user_id": 1,
      "text": "baz",
      "created_at": "2017-01-05 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 2,
      "user_id": 1,
      "text": "qux",
      "created_at": "2017-01-17 00:00:00",
      "updated_at": "2017-01-18 00:00:00"
    },
    {
      "id": 1,
      "user_id": 1,
      "text": "quux",
      "created_at": "2017-01-16 00:00:00",
      "updated_at": "2017-01-18 00:00:00"
    }
  ],
  "has_previous": false,
  "previous_cursor": null,
  "has_next": true,
  "next_cursor": {
    "updated_at": "2017-01-18 00:00:00",
    "created_at": "2017-01-14 00:00:00",
    "id": 6
  }
}

问题:关于 元组比较 呢?

有了这个特性,SQL 语句应该更简单。然而,根据 SQL 功能比较,一些 RDBMS,例如 SQLServer,不支持这种语法。因此,Lampager 持续使用冗余语句。

注意:请参阅 lampager/lampager

  • PaginatorProcessorPaginationResult 中的所有 camelCase 方法都可以用 snake_case 风格调用。

API

注意:请参阅 lampager/lampager

Paginator::__construct()
Paginator::create()

创建一个新的分页器实例。
但是,如果您使用全局函数 lampager(),则不需要直接实例化。

static Paginator create(\ORM|\ORMWrapper $builder): static
Paginator::__construct(\ORM|\ORMWrapper $builder)

Paginator::transform()

将 Lampager 查询转换为 Illuminate 构建器。

Paginator::transform(\Lampager\Query $query): \ORM|\ORMWrapper

Paginator::build()

执行配置 + 转换。

Paginator::build(\Lampager\Contracts\Cursor|array $cursor = []): \ORM|\ORMWrapper

Paginator::paginate()

执行配置 + 转换 + 处理。

Paginator::paginate(\Lampager\Contracts\Cursor|array $cursor = []): \Lampager\idiorm\PaginationResult

参数

  • (mixed) $cursor
    包含 $column => $value 的关联数组或实现 \Lampager\Contracts\Cursor 的对象。它必须是 全有或全无
    • 对于初始页面,省略此参数或传递空数组。
    • 对于后续页面,传递所有参数。不允许部分参数。

返回值

例如。

object(Lampager\Idiorm\PaginationResult)#1 (5) {
  ["records"]=>
  array(5) {
    [0]=>
    object(ORM)#2 (22) { ... }
    [1]=>
    object(ORM)#3 (22) { ... }
    [2]=>
    object(ORM)#4 (22) { ... }
    [3]=>
    object(ORM)#5 (22) { ... }
    [4]=>
    object(ORM)#6 (22) { ... }
  }
  ["hasPrevious"]=>
  bool(false)
  ["previousCursor"]=>
  NULL
  ["hasNext"]=>
  bool(true)
  ["nextCursor"]=>
  array(2) {
    ["updated_at"]=>
    string(19) "2017-01-18 00:00:00"
    ["created_at"]=>
    string(19) "2017-01-14 00:00:00"
    ["id"]=>
    int(6)
  }
}

Paginator::useFormatter()
Paginator::restoreFormatter()
Paginator::process()

调用处理器方法。

Paginator::useFormatter(\Lampager\Formatter|callable $formatter): $this
Paginator::restoreFormatter(): $this
Paginator::process(\Lampager\Query $query, array|\IdiormResultSet $rows): \Lampager\idiorm\PaginationResult

PaginationResult::toArray()
PaginationResult::jsonSerialize()

将对象转换为数组。

重要:将 camelCase 属性转换为 snake_case 形式。

PaginationResult::toArray(): array
PaginationResult::jsonSerialize(): array

PaginationResult::__call()

调用 IdiormResultSet 方法。

PaginationResult::__call(string $name, array $args): mixed