【问题标题】:Search with two fields not mandatory使用两个非必填字段进行搜索
【发布时间】:2015-11-03 09:07:19
【问题描述】:

我想在我的网站上做一个搜索功能。这项研究将根据产品名称和卖家位置显示产品。 我的看法:

<?php echo $this->Form->create('Product', array('type' => 'GET')); ?>

<div class="col col-sm-4">
    <?php echo $this->Form->input('search', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off', 'value' => $search)); ?>
</div>
    <div class="col col-sm-2">
        <?php echo $this->Form->input('searchCity', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off')); ?>
    </div>
<div class="col col-sm-3">
    <?php echo $this->Form->button('Search', array('div' => false, 'class' => 'btn btn-sm btn-primary')); ?>
</div>

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

两个搜索字段:一个用于名称,第二个用于位置。

我的控制器(仅适用于一个搜索字段):

if(!empty($this->request->query['search']) || !empty($this->request->data['name'])) {
        $search = empty($this->request->query['search']) ? $this->request->data['name'] : $this->request->query['search'];
        $search = preg_replace('/[^a-zA-Z0-9 ]/', '', $search);
        $terms = explode(' ', trim($search));
        $terms = array_diff($terms, array(''));
        $conditions = array(
            'Brand.active' => 1,
            'Product.active' => 1,
            'Product.date_discount >' => date('Y-m-d H:i:s')
        );
        foreach($terms as $term) {
            $terms1[] = preg_replace('/[^a-zA-Z0-9]/', '', $term);
            $conditions[] = array(
                'OR' => array(
                    array('Product.name LIKE' => '%' . $term . '%'),
                    //array('Brand.city LIKE' => '%' . $this->request->query['searchCity'] . '%')
                )
            );
        }
        $products = $this->Product->find('all', array(
            'recursive' => -1,
            'contain' => array(
                'Brand'
            ),
            'conditions' => $conditions,
            'limit' => 200,
        ));
        if(count($products) == 1) {
            return $this->redirect(array('controller' => 'products', 'action' => 'view', 'slug' => $products[0]['Product']['slug']));
        }
        $terms1 = array_diff($terms1, array(''));
        $this->set(compact('products', 'terms1'));
    }

没有必填的搜索字段。如果只有名称搜索字段,它将根据产品名称显示所有产品,无论位置如何。如果只有城市搜索字段,它将显示该位置的所有产品。如果两者都有,它将根据名称和位置显示所有产品。

我不知道要修改什么。我尝试在上面发布的 if 下再做一个if(!empty($this-&gt;request-&gt;query['searchCity']),但它没有用(复制粘贴第一个 if 并且我更改了条件)。 我该怎么办?

谢谢。

【问题讨论】:

    标签: php cakephp search


    【解决方案1】:

    你可以用这个CakeDC Search plugin

    首先将插件包含在您的应用中

    CakePlugin::load('Search');
    

    然后在你的模型中包含行为

    public $actsAs = array(
       'Search.Searchable'
    );
    

    public $filterArgs = array(
        'search' => array(
            'type' => 'like'
        ),
        'searchcity' => array(
            'type' => 'like'
        )
    );
    

    在你的控制器中

    public $components = array(
        'Search.Prg','Paginator'
    );
    
    public function find() {
        $this->Prg->commonProcess();
        $this->Paginator->settings['conditions'] = $this-> Product->parseCriteria($this->passedArgs);
        $this->set('products', $this->Paginator->paginate());
    }
    

    完整示例请访问here

    【讨论】:

    • 感谢您的回复!我尝试使用插件,但出现错误:在我的控制器中使用$this-&gt;set('products', $this-&gt;Paginator-&gt;paginate());“调用非对象上的成员函数 paginate()”
    • 而searchCity需要在另一个Model中搜索。有可能吗?
    • 在 $components 中添加 Paginator,就像我添加的一样
    • 是的,可以在相关模型中进行搜索。看看github.com/CakeDC/search/blob/master/Docs/Documentation/…
    • 好的,太好了!感谢您的帮助!
    【解决方案2】:

    我无法准确理解所询问的内容,但据我了解,OR 条件没有按预期工作,这是因为您的$conditions 中使用的语法存在一个小问题。不需要用于条件中每个字段的其他数组,这会导致问题。所以,

    代替:

    $conditions[] = array(
        'OR' => array(
            array('Product.name LIKE' => '%' . $term . '%'),
            array('Brand.city LIKE' => '%' . $this->request->query['searchCity'] . '%')
        )
    );
    

    使用:

    $conditions[] = array(
        'OR' => array(
            'Product.name LIKE' => '%' . $term . '%',
            'Brand.city LIKE' => '%' . $this->request->query['searchCity'] . '%'
        )
    );
    

    另外,为静态条件和动态条件使用两个不同的变量($termsforeach()),然后使用array_combine 将它们组合起来,如下所示:

    $conditions_st = array(
        'Brand.active' => 1,
        'Product.active' => 1,
        'Product.date_discount >' => date('Y-m-d H:i:s')
    );
    
    
    foreach($terms as $term) {
        $terms1[] = preg_replace('/[^a-zA-Z0-9]/', '', $term);
        $conditions_dy = array(
            'OR' => array(
                'Product.name LIKE' => '%' . $term . '%',
                'Brand.city LIKE' => '%' . $this->request->query['searchCity'] . '%'
            )
        );
    }
    
    $conditions = array_combine($conditions_st, $conditions_dy);
    

    P.S:我刚刚浏览了您的代码,并不太明白您要做什么。我只是强调了我注意到的问题。

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2021-06-22
      • 2014-08-10
      • 2013-04-07
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      相关资源
      最近更新 更多