沃梦达 / 编程问答 / php问题 / 正文

使用 AWS DynamoDB 和 PHP 进行分页

Pagination with AWS DynamoDB with PHP(使用 AWS DynamoDB 和 PHP 进行分页)

本文介绍了使用 AWS DynamoDB 和 PHP 进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让某人知道如何对表中的记录进行分页.其实我想用 DynamoDb 在 php 中创建一个分页组件.

似乎不可能像

1,2,3,4,5... 这样的分页.

因为 Dyanmodb 只是为我们提供了 LIMIT 子句,我们可以通过它来读取某些不.记录,我们可以通过 LastEvaluatedKey 处理接下来的 n 条记录.所以如果我想直接跳到第 5 页,怎么可能?

据我所知,我们无法在分页中显示页码.我们能做的就是读取一定限制的记录并提供NEXT链接来检索下n条记录.

分页是任何Web应用程序的基本功能,如果迁移到DynamoDb等云数据库,我们如何实现?

请提供您的意见和建议.谢谢

解决方案

是的,你说得对,DynamoDB 中没有 OFFSET.但是只使用 LimitLastEvaluatedKey,我做了这个功能:

public function scan($table, $filter = [], $select = null, $limit = 2){$page = isset($_GET['page']) ?$_GET['页面'] : 0;$选项= ['表名' =>$表,'计数' =>真的,];如果(!空($限制)){$options['Limit'] = $limit;}如果 (!is_null($select)) {$options['Select'] = $select;}如果(!空($过滤器)){$options['ScanFilter'] = $filter;}$results = $results = $this->_client->scan($options);而 ($page > 0 && isset($results['LastEvaluatedKey'])) {$results = $results = $this->_client->scan($options);$options['ExclusiveStartKey'] = $results['LastEvaluatedKey'];$页面--;}返回 $results;}

$this->_client 指的是 DynamoDb 客户端对象.
基本上,我使用 LastEvaluatedKey 遍历所有条目,直到到达所需页面.
要获取表中的总条目,请调用 $this->scan($this->tableName(), [], null, null)['Count']; (即 - 没有任何搜索条件并且没有分页,就像在正常分页功能中一样).

Have someone any idea about Paginating the records from a table. Actually I want to create a paginate component in php with DynamoDb.

It seems like it is not possible to giving pagination like <first> <prev> 1,2,3,4,5... <next> <last>.

Because Dyanmodb just provide us LIMIT clause by which we can read certain no. of records and we can process next n records by LastEvaluatedKey. So if I want to jump directly to 5th page, How is it possible ?

As per my understanding we can't display page numbers into the pagination. The thing we can do is just read certain limit of records and provide the NEXT link to retrieve next n records.

Pagination is basic feature of any web application, How can we implement if migrating to cloud database like DynamoDb ?

Please provide your views and suggestions. Thanks

解决方案

Yes, you are right, there is no OFFSET in DynamoDB. But using only Limit and LastEvaluatedKey, i made this function:

public function scan($table, $filter = [], $select = null, $limit = 2)
{
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    $options = [
        'TableName' => $table,
        'Count' => true,
    ];

    if (!empty($limit)) {
        $options['Limit'] = $limit;
    }

    if (!is_null($select)) {
        $options['Select'] = $select;
    }

    if (!empty($filter)) {
        $options['ScanFilter'] = $filter;
    }

    $results = $results = $this->_client->scan($options);

    while ($page > 0 && isset($results['LastEvaluatedKey'])) {
        $results = $results = $this->_client->scan($options);
        $options['ExclusiveStartKey'] = $results['LastEvaluatedKey'];
        $page--;
    }

    return $results;
}

$this->_client refers to DynamoDb client object.
Basically i loop through all entries with LastEvaluatedKey till i reach needed page.
To get total entries in table, call $this->scan($this->tableName(), [], null, null)['Count']; (that is - without any search criteria and without pagination, just as in normal pagination function).

这篇关于使用 AWS DynamoDB 和 PHP 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 AWS DynamoDB 和 PHP 进行分页

基础教程推荐