cakephp搜索插件基本使用

时间:2015-06-09 12:47:10

标签: cakephp cakephp-search-plugin cakephp-2.x

我希望你能帮助我。我已完成所有步骤,如“cakephp basic help to use cakedc search plugin”,但搜索者没有任何内容。

我的项目是关于一个用户列表,我想过滤'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>
你能告诉我我做错了吗?非常感谢!!

贝伦

0 个答案:

没有答案