Kom*_*ala 4 cakephp cakephp-2.0
我已经实现了一个用户模型,我有一个简单的搜索表单,可以按性别,年龄,位置进行搜索.我正在使用paginator对结果进行分页.问题是它只记住第一页上的搜索条件.当我点击第2页或下一页等时.它默认返回搜索所有用户.
这是我的控制器中的脏搜索代码,基本上它只检查提交的表单字段并对Users表中的匹配字段进行查询,然后对结果进行分页:
if (!empty($this->data)) {
// by name
if (!empty($this->data['User']['search_name'])) {
$this->paginate = array('conditions' => array('visible'=>1,
'OR'=>array(
'User.username LIKE' => '%'.$this->data['User']['search_name'].'%',
'User.firstname LIKE' => '%'.$this->data['User']['search_name'],
'User.lastname LIKE' => '%'.$this->data['User']['search_name'])
), 'limit'=>'10', 'order'=>'User.username');
}
// by gender
else if (!empty($this->data['User']['search_gender'])) {
$this->paginate = array('conditions' => array(
'visible'=>1,
'User.gender' => $this->data['User']['search_gender']
), 'limit'=>'10', 'order'=>'User.username');
}
// by state
else if (!empty($this->data['User']['search_state'])) {
$this->paginate = array('conditions' => array(
'visible'=>1,
'User.state' => $this->data['User']['search_state']
), 'limit'=>'10', 'order'=>'User.username');
}
// Send the results for the above criteria to the view
$results = $this->paginate('User');
$this->set('users', $results);
}
// Default retrieval of all users
else {
$this->paginate = array('conditions'=>array('visible'=>1),
'limit'=>'10', 'order'=>'User.username');
$this->set('users', $this->paginate('User'));
}
Run Code Online (Sandbox Code Playgroud)
我正在试图弄清楚如何使分页的后续页面记住我的搜索条件.谢谢你的帮助.
$this->data 在第二页上是空的,因为它是通过发布您的搜索表单(未发布到第二页)来填充的.
将您的搜索表单帖子更改为GET($this->Form->create('ModelName', array('type'=>'get'))并解析$this->params而不是$this->data