【问题标题】:How can I limit my database request according to the pagination of the datatable?如何根据数据表的分页限制我的数据库请求?
【发布时间】:2020-05-21 12:47:53
【问题描述】:

我正在从我的数据库加载数据,这是一个非常大量的数据。为了获得真正的快速性能,我限制了数据(使用我的parts 函数)

控制器.php

$table = $this->em->getRepository($EntityName)->parts(10);
$table_json = $serializer->serialize($table, 'json');
$table_array = json_decode($table_json);
// here I am adding some complex data to the array from other database tables and then I encode the array again for the datatable
$data = json_encode($table_array);

page.html.twig:

  var table = $('.table').DataTable({
    "lengthMenu": [[10, 25, 50,100, -1], [10, 25, 50,100, "All"]],
    "pageLength": 10,
    "scrollX": true,
    "data":{{ output.data|raw }},

零件功能:

 public function parts($limit) {
      return $this->createQueryBuilder('documents')
      ->setMaxResults($limit)
      ->getQuery()
      ->execute();
    }

我想知道的是,根据分页,我是否有可能从我的数据库中仅加载 10 条记录?因此,分页 1 到 10 将加载前 10 条记录,而分页 11 到 20 将加载接下来的 10 条记录。我进行了很多研究,但没有找到适合我的特定代码的解决方案。这是因为在从数据库加载之后,我需要先对记录进行一些处理,然后再将其显示在数据表中。

【问题讨论】:

    标签: php json symfony datatables limit


    【解决方案1】:
    public function parts($offset, $limit) {
        return $this->createQueryBuilder('documents')
          ->setFirstResult($offset)
          ->setMaxResults($limit)
          ->getQuery()
          ->getResult();
    }
    

    但我建议使用KnpPaginatorBundle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-25
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多