我希望你能帮助我。我已经完成了这里的所有步骤“使用 cakedc 搜索插件的 cakephp 基本帮助”,但搜索器什么也没做。
我的项目是关于一个用户列表的,我想过滤“apellidos”或“dni”字段。
在我的控制器中,我声明了组件,并且:
public function index() {
$this->Prg->commonProcess();
$this->paginate = array(
'conditions' => $this->User->parseCriteria($this->passedArgs));
$this->set('users', $this->paginate());
$this->User->recursive = 0;
$this->User->order = 'User.cod_cliente';
$this->User->conditions = 'User.nombre';
if($this->Session->read('Auth.User.group_id')==5)
{
//$this->set('categorias', $this->User->find('all',array('fields'=>array('group_id','cod_cliente','nombre','apellidos','dni','id'))));
}else
{
$this->User->conditions = array('User.cod_centro'=>$this->Session->read('Auth.User.cod_centro'));
//$this->set('categorias', $this->User->find('all',array('fields'=>array('group_id','cod_cliente','nombre','apellidos','dni','id'),'conditions'=>array('User.cod_centro'=>$this->Session->read('Auth.User.cod_centro')))));
}
$this->set('categorias', $this->Paginator->paginate());
$this->set('title_for_layout', __('Listado de usuarios')." - ".__('Administración'));
}
public $presetVars = array(
array('field' => 'apellidos', 'type' => 'value'),
array('field' => 'dni', 'type' => 'value'),);
public $actsAs = array('Containable','Search.Searchable','Acl' => array('type' => 'requester'));
public $filterArgs = array(
array('name' => 'apellidos', 'type' => 'query', 'method' => 'filterApellidos'),
array('name' => 'dni', 'type' => 'query', 'method' => 'filterDni'),
);
这里还有功能:
public function filterApellidos($data, $field = null) {
if (empty($data['apellidos'])) {
return array();
}
$apellidosField = '%' . $data['apellidos'] . '%';
return array(
'OR' => array(
$this->alias . '.apellidos LIKE' => $apellidosField,
));
}
public function filterDni($data, $field = null) {
if (empty($data['dni'])) {
return array();
}
$dniField = '%' . $data['dni'] . '%';
return array(
'OR' => array(
$this->alias . '.dni LIKE' => $dniField,
));
}
// Built a list of search options (unless you have this list somewhere else)
public function __construct($id = false, $table = null, $ds = null) {
$this->statuses = array(
'' => __('All', true),
0 => __('Bid', true),
1 => __('Cancelled', true),
2 => __('Approved', true),
3 => __('On Setup', true),
4 => __('Field', true),
5 => __('Closed', true),
6 => __('Other', true));
parent::__construct($id, $table, $ds);
}
最后,在视图中我有这个:
<div><?php
echo $this->Form->create('Usuario', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('apellidos', array('div' => false, 'empty' => true)); // empty creates blank option.
echo $this->Form->input('dni', array('div' => false, 'empty' => true));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
</div>
你能告诉我我做错了什么吗?非常感谢!!
贝伦