2015-09-03 91 views
-2

我有一個搜索輸入來搜索標題字段,我想通過titledescription字段搜索具有相同輸入的字段,這是可行的嗎?CakePHP在兩個字段中搜索

這是我的搜索funtion:

$query = $this->request->query['search']; 
$category_id = $this->request->query['category_id']; 
$multimedia_type_id = $this->request->query['multimedia_type_id']; 

$conditions = array(

    'conditions' => array(   
    'and' => array())); 

    if ($query) $conditions['conditions']['and']['Multimedia.title LIKE' ] = "%$query%"; 
     if ($category_id) $conditions['conditions']['and']['Multimedia.category_id'] = $category_id ; 
     if ($multimedia_type_id) $conditions['conditions']['and']['Multimedia.multimedia_type_id'] = $multimedia_type_id ; 

我想在同一時間

回答

2

你想怎麼說就titlesdescription搜索的是,你需要可以與描述或搜索標題。這是一個OR而不是一個and。然後將此OR與額外的and結合。

所以檢查:

$conditions = array(
    'AND' => array(
     'OR' => array(
      'Multimedia.title LIKE' => "%$query%", 
      'Multimedia.description LIKE' => "%$query%", 
     ), 
     'AND' => array(
      'Multimedia.multimedia_type_id' => $multimedia_type_id 
      'Multimedia.category_id' => $category_id 
     ) 
    ) 
); 
+0

我必須把這個: 'Multimedia.title', 'Multimedia.description' 數組裏面()? – GusDev

+0

是的......爲什麼不呢? – gmponos

+0

它的工作原理,謝謝 – GusDev