我是CakePHP的新手,但是我开始涉足它。我在项目中添加搜索功能时遇到了一些问题。我尝试添加各种YouTube教程的说明,并尝试对其进行修改以在我的项目中工作,但似乎没有任何效果。我已经感谢您的帮助!
这是我的控制器代码:
class ProductsController extends AppController {
public function index() {
/* $keyword = $this->request->query('keyword');
if(!empty($keyword)){
$this-set('products', $this->Product->find());
} */
$keyword = $this->request->data('keyword');
if($keyword != null)
{
$this->set('products', $this->Product->find('all', array('conditions' => '%'.$keyword.'%')));
}
else {
$this->set('products', $this->Product->find('all'));
}
}
这是我的索引文件(主页):
<div id="listaus">
<?= $this->Html->link('+ Listaa Uusi Auto', ['action' => 'add']) ?>
<?= $this->Form->create("", array('type' => 'get')); ?>
<?= $this->Form->control('keyword'); ?>
<?= $this->Form->end(__('Search')); ?>
</div>
<div class="row">
<?php foreach ($products as $product):?>
<div class="col-sm-6 col-md-4">
<div class="">
<?php echo $this->Html->link($this->Html->image($product['Product']['kuva']),
array('action'=>'view',$product['Product']['id']),
array('escape'=>false,'class'=>'thumbnail'));?>
<div class="caption">
<h4>
<?php echo $product['Product']['name'];?>
</h4>
<h5>
Price:
<?php echo $product['Product']['hinta'];?>
€
</h5>
</div>
</div>
</div>
<?php endforeach;?>
</div>
希望有人能解决我的问题,谢谢! :)
答案 0 :(得分:0)
似乎您没有添加LIKE子句来匹配搜索条件。
尝试更改
$this->set('products', $this->Product->find('all', array('conditions' => '%'.$keyword.'%')));
至
$this->set('products',
$this->Product->find('all',
array(
'conditions' =>
array(
'Product.name LIKE ' => '%'.$keyword.'%'
)
)
)
);