我正在使用此操作来搜索用户名
在我的用户表上运行正常
但是当我尝试在联系表上执行相同操作时 我正在获取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>
我想念什么?
答案 0 :(得分:0)
您正在重新分配分页数组(我很确定还有类似的问题,但现在找不到)
事实上,当你这样做
$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使用查询值来填充控件值答案 1 :(得分:0)
谢谢你,你是对的 我通过添加else来解决
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']
];
}