搜索在 cakephp-3 中不起作用

searching not work in cakephp-3

我想通过搜索输入从数据库中搜索我的数据,一切看起来都正确,但我找不到为什么这个代码不是 work.when 我在输入框中输入搜索数据任何东西都是 done.please帮助

我的控制器:AdUnitsController

 class AdUnitsController extends AppController

{

public function index()
{

    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];

    $adUnits = $this->paginate($this->AdUnits);

    $this->set(compact('adUnits'));
    $this->set('_serialize', ['adUnits']);

}

public function _getData()
{
    if (!empty($this->request->data['search'])) {
        $search = $this->request->data['search'];
        $query = $this->AdUnits->find('All')->where([
            'OR' => [
                ['name LIKE' => '%' . $search . '%'],
            ],
        ]);
    }
    else{
        $query = $this->AdUnits->find('All');
    }
    return $query;
}

我的观点index.ctp:

        <label> Search</label>
        <?php echo $this->Form->create('AdUnits',array('id' => 'site-search','url'=>array('action'=>'index')));?>
        <div>
            <?php echo $this->Form->input('search',array('class' => 'form-control', 'label' =>false, 'placeholder' => 'Type Network')); ?>
        </div>

        <div> <button type="submit"> Search</button>

            <?php echo $this->Form->end();?>

那里搜不到work.please帮助

public function index()
{

    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];

    $adUnits = $this->paginate($this->AdUnits);

    $this->set(compact('adUnits'));
    $this->set('_serialize', ['adUnits']);

}

替换为

    public function index()
{
    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];
    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

}

首先简化您的代码:您无缘无故地进行了两次分页

public function index()
{
    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];
    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

} 

如果您仍然遇到问题,可能是您的查询有误。我的猜测是 name 列不明确,所以在你的 find() 调用中这样做

['AdUnits.name LIKE' => '%' . $search . '%']

作为旁注,我建议您阅读文档,特别是学习如何调试代码。

在我看来,_getData 方法也应该移到模型或组件中。