我有一个功能,可以在数据库中搜索所有带有用户搜索等描述的产品,但它不起作用。问题出在哪儿?
public function searchProduct(){
$search = $this->request->query('search');
$products = $this->Product->find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
if($this->Auth->user()!= null){
$userId = $this->Auth->user('id');
$user = $this->User->findById($userId);
$filters = $user['Filter'];
$arrayFiltersOfUser = array();
$arrayOfProductIds = array();
for($i = 0; $i < count($filters); $i++){
$arrayFiltersOfUser[$i] = $filters[$i]['id'];
}
for($i = 0; $i < count($products); $i++){
$arrayOfProductIds[$i] = $products[$i]['Product']['id'];
}
for($i = 0; $i < count($arrayOfProductIds); $i++){
$arrayFiltersOfProduct_aux[$i] = $this->FiltersProduct->findAllByProductId($arrayOfProductIds[$i]);
$array = $arrayFiltersOfProduct_aux[$i];
for($j = 0; $j < count($array); $j++){
$array2[$j] = $array[$j]['FiltersProduct']['filter_id'];
}
$containsAllValues = !array_diff($arrayFiltersOfUser, $array2);
if($containsAllValues == true){
$this->set("is_valid", "yes");
$this->set("_serialize", array("is_valid"));
}
else{
$this->set("is_valid", "no");
$this->set("_serialize", array("is_valid"));
}
}
}
$this->set("response", $products);
$this->set("_serialize", array("response"));
}
答案 0 :(得分:1)
您现在正在搜索:%$ search%而不是$ search的值。
尝试更换
find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
同
find("all",array('conditions'=>array('Description LIKE'=>"%".$search."%")));
这应该可以解决问题。