我有一个索引视图,该视图显示了所有产品并且运行良好,我想在顶部有一个搜索栏,用户可以在其中搜索关键字。我的控制器如下所示:
class ProductsController extends AppController{
public function search(){
$this->loadModel('Item');
$this->view('index');
$keyword = $this->request->data['Item']['keywords'];
$this->Product->recursive = 0;
$this->paginate = array(
'conditions' => array('Item.is_deleted' => false, 'Item.name LIKE' => "%$keyword", 'Item.item_type_id' => 15)
);
$this->set('products', $this->Paginator->paginate());
$this->redirect(array('action' => 'index'));
}
}
显示项目是因为产品是一个项目,并且我有两个表,但我只想按关键字进行过滤应该不是问题。
这是索引视图:
<div class="products index">
<h2><?php echo __('Products'); ?></h2>
<?php echo $this->Form->create('Product', array('url' => 'search')) ?>
<fieldset>
<legend><?php echo __('Search Products') ?></legend>
<?php
echo $this->Form->input('Item.keywords');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')) ?>
//other stuff for index view
但是,即使我在提交表单时使该视图在搜索控制器中呈现,我仍然得到“在此服务器上找不到所请求的地址'/ WarehouseManagementApp / products / search'。”因此,也许我缺少了一些东西,或者也许我应该以不同的方式实现它。
因此,为此拥有一个单独的功能实际上是愚蠢的,我所做的全部都放在索引函数中,并检查是否调用了搜索按钮(是否张贴):
public function index() {
if ($this->request->is(array('post', 'put'))) {
$keyword = $this->request->data['Item']['keywords'];
$this->Product->recursive = 0;
$this->paginate = array(
'conditions' => array('Item.is_deleted' => false, 'Item.name LIKE' => '%' . $keyword . '%')
);
$this->set('products', $this->Paginator->paginate());
}else{
$this->Product->recursive = 0;
$this->paginate = array(
'conditions' => array('Item.is_deleted' => false)
);
$this->set('products', $this->Paginator->paginate());
}
}