【问题标题】:CakePHP implementing search inside a viewCakePHP 在视图中实现搜索
【发布时间】:2019-10-29 21:59:08
【问题描述】:

我有一个显示所有产品并且工作正常的索引视图,我想在顶部有一个搜索栏,用户可以通过关键字搜索。我的控制器如下所示:

    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'”。 所以也许我遗漏了一些东西,或者我应该以不同的方式实现它。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    所以实际上为此有一个单独的函数是愚蠢的,我所做的只是将它全部放在索引函数中并检查是否调用了搜索按钮(是否发布):

    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());
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多