我有一个应用程序从下拉列表中获取值并使用它来过滤索引视图。以下是控制器代码:
public function browse() {
$this->loadModel('ReqMissionId');
$missions = $this->ReqMissionId->find('list', array('fields' => array('mission_id', 'mission_id')));
$this->set(compact('missions'));
}
这是索引的控制器代码:
public function index() {
if($this->request->is('post')) {
$options = array('Requirement.mission_id' => $this->request->data['Requirement']['mission_id']);
$this->set('mission_id', $this->request->data['Requirement']['mission_id']); //Used to display the mission_id on the index page.
$this->Paginator->settings = $this->paginate;
$this->set('requirements', $this->Paginator->paginate('Requirement', $options));
}
}
View中的下拉菜单很简单:
<?php
echo $this->Form->input('mission_id', array('label' => 'Mission ID'));
echo $this->Form->submit(__('View'));
echo $this->Form->end();
?>
我的问题是分页。由于Cake在对列进行排序时进行回发,因此'Requirement.mission_id'的值丢失,我得到“调用成员函数sort()on null”错误。我觉得我可以使用会话变量处理这个问题,但我不清楚实现。这是正确的,还是我应该做的其他事情?
答案 0 :(得分:1)
检查您正在分页的模型的会话。尝试这样的事情:
尝试这样的事情:
if(!empty($this->request->data['Requirement']))
$this->Session->write('Requirement', $this->request->data['Requirement']);
else
$this->request->data['Requirement'] = $this->Session->read('Requirement');
将其纳入您的$选项。尝试使用这样的数组合并:
$options = array_merge($options, $this->getOptions($this->request->data));
在以下位置实施getOptions:
function getOptions($data = null){
$options = array();
if(!empty($data['Requirement']['mission_id']))
$options = array_merge($options, array('mission_id' => $data['Requirement']['mission_id']));
return $options;
}