【问题标题】:Pagination Sort in Cakephp 3.xCakephp 3.x 中的分页排序
【发布时间】:2015-05-10 23:20:44
【问题描述】:

在 cakephp 3.x 中,我无法在查找中进行分页顺序

这是我的控制器:

//AgentsController.php
public function show()
{
    $agents = $this->Agents->find()
    $this->set('agents', $this->paginate($agents));
}

这是我的部分观点

//show.ctp
<!-- ....... -->
<table class="table table-striped">
   <thead>
      <tr>
        <th>
            <?php echo $this->Paginator->sort('full_name', 'Nome', array('escape' => false)); ?>
        </th>
        <th>
            <?php echo $this->Paginator->sort('username', 'Email', array('escape' => false)); ?>
        </th>
        <th>
            <?php echo $this->Paginator->sort('regions', 'Regioni', array('escape' => false)); ?>
        </th>
      </tr>
   </thead>
<!-- ....... -->

我哪里错了?

【问题讨论】:

  • 您的意思是单击排序链接之一时?如果是这样,生成的 url 是什么?
  • /admin/agents/show?sort=username&direction=asc
  • 你能把生成的 SQL 显示在 debukit 中吗?
  • 没有order by。只有 Select、From、Join、Limit 和 offset
  • 用户名是代理表的一列吗?

标签: php cakephp cakephp-3.0


【解决方案1】:

分页器将阻止任何按您正在使用的查询的主表中不存在的列进行排序的尝试。在这种情况下,您有 2 个选项。第一个选项是更改排序链接以告诉 cake 该列属于相关表:

<?php echo $this->Paginator->sort('Users.full_name', 'Nome'); ?>

或者您可以在组件中告诉它允许按给定的一组列进行排序:

$this->paginate = ['sortWhitelist' => ['full_name', 'username']]

【讨论】:

【解决方案2】:

试试:

$this->paginate = ['sortWhitelist' => ['full_name','username','regions'],'limit' => 3];

这也会设置分页限制。

【讨论】:

  • 这与 6 个月大的接受答案几乎相同,只是更改了限制(不在问题中)。不是一个有用的答案。
猜你喜欢
  • 2016-12-29
  • 2021-02-19
  • 1970-01-01
  • 2016-01-02
  • 2012-05-10
  • 1970-01-01
  • 2012-03-08
  • 2015-06-19
  • 2016-05-15
相关资源
最近更新 更多