搜索函数不工作-Cakephp

搜索函数不工作-Cakephp,cakephp,search,Cakephp,Search,我正在使用此操作搜索用户名 它在我的用户表上运行良好 但当我试着在联系表上做同样的事情时 我正在获取url,但它没有检索结果 ContactController.php public function index() { $search = $this->request->query('search'); if(!empty($search)){ $this->paginate = [ 'conditions' =>[

我正在使用此操作搜索用户名

它在我的用户表上运行良好

但当我试着在联系表上做同样的事情时 我正在获取url,但它没有检索结果

ContactController.php

 public function index()
{
  $search = $this->request->query('search');
    if(!empty($search)){
        $this->paginate = [
            'conditions' =>['Contacts.name LIKE '=>'%'.$search. '%']

        ];
    }    
  $this->paginate = [
        'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status', 'Secteurs', 'Products']
    ];
    $contacts = $this->paginate($this->Contacts);
    $this->set(compact('contacts'));
    $this->set('_serialize', ['contacts']);  

}
index.ctp

 <div class="col-md-2 col-sm-2 col-xs-12 form-group top_search">
              <div class="input-group">

                <span class="input-group-btn">
                    <?= $this->Form->create("",['type'=>'get']) ?>
                    <?= $this->Form->control('search', ['default'=>$this->request->getQuery('search'),'class'=>'form-control','placeholder'=>'Search For Name'
                  ]); ?>
                    <button class="btn btn-default ">Go!</button>
                    <?= $this->Form->end() ?>
                                </span>
                </div>
                </div>

走!

我遗漏了什么?

您正在重新分配分页数组(我很确定有类似的问题,但我现在找不到)

事实上,当你这样做的时候

$this->paginate = [
    'contain' => [...]
];
正在重新分配$this->paginate的值

以下几点应该有效

 $this->paginate = [
    'contain' => [/* your tables here */]
];

if(!empty($search)) {
    $this->paginate['conditions'] =>['Contacts.name LIKE '=>'%'.$search. '%']
}    
但您可以改进代码:

  • $this->request->query('search')
    已弃用,请使用
    getQuery()
  • 无需在视图中使用
    'default'=>$this->request->getQuery('search')
    。如果您使用
    $this->Form->create(null,['valueSources'=>'query']),FormHelper将为您执行此操作这告诉formHelper使用查询值填充控件值
  • 最后考虑使用FooSkfBoo/搜索插件来完成所有的工作
      谢谢,你说得对 我通过添加其他内容来修复

       public function index()
        {
           $search = $this->request->query('search');
      
          if(!empty($search)){
              $this->paginate = [
                  'conditions' =>['Contacts.name LIKE '=>'%'.$search. '%']
      
              ];
          }else{
      
          $this->paginate = array(
              'limit' => 4 );
            [
          'contain' => ['Users', 'Contacts', 'SourceProspects', 'Status', 'Secteurs', 
      'Products']
      
          ];
        }
      

      你到底有什么问题?空白页?空记录集?您是否启用了调试模式?您是否看到已执行查询?请添加更多信息我的问题它没有显示所需的结果它显示了来自表localhost/intellix/contacts的所有数据?search=zak我尝试在这里调试$search=$this->request->query('search');它不显示结果zak,但不在我的表视图中(重定向),它一直显示所有数组