我想在我的网站上执行搜索功能。本研究将根据产品的名称和卖家的位置展示产品。 我的观点:
<?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(!empty($this->request->query['searchCity'])
,但它没有工作(复制粘贴第一个if和I更改条件)。
我该怎么办?
感谢。
答案 0 :(得分: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
答案 1 :(得分:0)
我无法准确理解被问到的内容,但根据我的理解,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'] . '%'
)
);
此外,对于静态条件和动态条件使用两个不同的变量($terms
中使用foreach()
),然后使用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:我刚刚浏览了您的代码,并且无法理解您的目标。我只是强调了我注意到的问题。