Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php drupal 8分页不适用于select查询_Php_Pagination_Drupal 8 - Fatal编程技术网

Php drupal 8分页不适用于select查询

Php drupal 8分页不适用于select查询,php,pagination,drupal-8,Php,Pagination,Drupal 8,我有一个drupal 8 select查询,它返回项目列表,我想给它添加分页,但寻呼机进入节点,但它没有正确分页,它显示了所有结果,这是我的代码 $sql = "SELECT nid FROM node WHERE type='used_item'"; $item_nids= \Drupal::database()->query($sql) ->fetchAllAssoc('nid');

我有一个drupal 8 select查询,它返回项目列表,我想给它添加分页,但寻呼机进入节点,但它没有正确分页,它显示了所有结果,这是我的代码

    $sql = "SELECT nid FROM node WHERE type='used_item'";
        $item_nids= \Drupal::database()->query($sql)
                    ->fetchAllAssoc('nid');
        $total=count($item_nids);
       $num_per_page='12';
    
        $pager = \Drupal::service('pager.manager')
          ->createPager($total, $num_per_page);
        $page = $pager
          ->getCurrentPage();
        
       
        // Load the item nodes from the list array.
        $item_nodes = Node::loadMultiple(array_keys($item_nids));
        // Build the items.
        $item_build = \Drupal::entityTypeManager()->getViewBuilder('node')- 
 >viewMultiple($item_nodes, 'carousel_card');
    
        $build[] = [
          '#theme' => 'full_list',
          '#name' => $name,
          '#description' => $description,
          '#item_cards' => $item_build,
        ];
        $build[] = [
          '#type' => 'pager',
        ];
        return $build;

如果我做错了什么,请告诉我。

请始终查看Drupal core,以获得如何使用API的更好示例。这里有两个可能有帮助的例子

您缺少的最明显的一点是,您没有扩展寻呼机,因此它无法更改您的查询

$query = db_select('node', 'n')
  ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
  ->fields('n', ['nid'])
  ->condition('n.type', 'used_item')
  ->id());
这就是它如何应用分页的“范围”