CakePHP 2.4.4无法使用paginate(Paginator)对虚拟字段进行排序/排序 [英] CakePHP 2.4.4 Unable to sort/order by virtual field with paginate (Paginator)

查看:291
本文介绍了CakePHP 2.4.4无法使用paginate(Paginator)对虚拟字段进行排序/排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Virtual Field for Property模型,在beforeFind()中生成,称为distance,可以工作。但是,我不能排序。我目前将此作为搜索表单的一部分:

My Virtual Field for Property model, generated in beforeFind(), called 'distance', works. However, I can't sort by it. I currently have this as part of the search form:

echo $this->Form->input('sort', array(
    'options' => Hash::get($predefined, 'sort'), // List of options
));

这会给URL添加排序,它适用于所有正常字段,但不适用于虚拟字段距离。我应该实现这个不同?据我所知,虚拟字段应该与分页正常工作。

Which adds sort to the URL, and it works for all normal fields, but not the virtual field distance. Should I have implemented this differently? As far as I'm aware, Virtual Fields should work fine with pagination.

编辑:如果我实现分页错误...

In case I've implemented pagination wrong...

我已设置默认值:

public $paginate = array(
    'limit' => 25,
    'order' => array(
       'Properties.id' => 'asc'
    )
);

建立$条件后:

$this->Paginator->settings = $this->paginate;
$data = $this->Paginator->paginate('Property', $conditions);
$this->set('properties', $data);

上面的工作可以很好地改变顺序,即使在URL中直接编辑/排序:fieldname /但不与虚拟字段距离。我也尝试添加,没有成功:

The above works fine changing the order, even by directly editing /sort:fieldname/ in the URL, but not with the virtual field 'distance'. I also tried adding, without success:

$this->paginate['order'] = array($value); //Value is the value for 'sort' named parameter


推荐答案

您的信息的这部分:


我的虚拟领域的属性模型,生成于beforeFind(),

My Virtual Field for Property model, generated in beforeFind(),

似乎是问题。

虚拟字段需要在调用Paginate以便分页器知道构建排序标准。

The virtual field needs to be defined before your call to Paginate() so that the paginator knows to build the "sort" criteria.

beforeFind()之后分页调用,因此您的虚拟字段尚未定义。将虚拟字段的定义移动到分页调用的上方应该可以解决这个问题。

The beforeFind() call happens after the pagination call, so your virtual field is not yet defined. Moving the definition of the virtual field right above the the pagination call should fix this.

这篇关于CakePHP 2.4.4无法使用paginate(Paginator)对虚拟字段进行排序/排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆