2012-03-19 9 views
0

検索フォームに複数のフィールドがあります。すべてのフィールドが空である可能性があります。 私はこのようなクエリを作成:検索パラメータがnullの場合の結果を返します。i Zend Lucene

$search_title = trim($_POST["search_title"]); 
$search_skill = trim($_POST["search_skill"]); 
$search_company = trim($_POST["search_city"]); 
$search_country_id = trim($_POST["search_county_id"]); 

$hits = $index->find("title:$search_title and skill:$search_skill and city:$search_city and country_id:$country_id"); 

ユーザーのみタイトルやスキルや都市などを埋めることができますが、いくつかのフィールドが空である場合、私は何の結果を持っていません。 すべてのフィールドが塗りつぶされて一致した場合にのみ結果が得られます。 一つだけのフィールドが満たされている場合は、そのフィールドを無視nullの場合、私は、結果を習慣:

$hits = $index->find("title: and skill: and city: and country_id:$country_id");  
+0

私はアイデアのために前に答えたこの似たような質問を見てください:http://stackoverflow.com/questions/9764950/mysql-updating-some-database-fields-without-overwriting-fields-not-changed/9765118#9765118 –

答えて

0

あなたはこのような何か試してみてください:あなたはあなたのフォームでStringTrimフィルタを使用する場合は勝った

if ($this->getRequest()->isPost()) { 
      if ($form->isValid($this->getRequest()->getPost)) { 
       //get filtered and valid values from search form. 
       //filter the array for set values 
       $data = array_filter($form->getValues()); 
       //extract key => vaules as $variable = values 
       extract($data); 
       $query1 = new Zend_Search_Lucene_Search_Query_MultiTerm(); 
       //test for variable isset and add term to query 
       if (isset($search_title)){ 
        $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_title, 'title')); 
       } 
       if (isset($search_skill)){ 
        $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_skill, 'skill')); 
       } 
       if (isset($search_city)){ 
        $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_city, 'city')); 
       } 
       if (isset($search_country_id)){ 
        $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_country_id, 'country_id')); 
       } 
       //This should give the AND you are looking for...I hope 
       $query= new Zend_Search_Lucene_Search_Query_Boolean(array($query1), array(TRUE)); 
       //get result set from query 
       $hits = $index->find($query); 
      } 
     } 

をデータにtrim()関数を使用する必要はありません。 $ _POST配列はユーザ​​が提供するデータで危険です.ZFは、指定したフィルタとバリデータを持っているRequestオブジェクト(POSTとGET)からデータを提供するための一連のメソッドを提供します(getValues())。
私はgetValues()を使用したので、この例ではextract()関数を使用しました。データはフィルタリングされ、検証されています。もちろん、変数にkey => valueのペアを割り当てる他の有効な方法もあります。あなたのお気に入りを使用してください。

+0

このクエリは、search_titleまたはsearch_skillまたはsearch_cityなどを返しますが、search_titleとsearch_skillなどが必要です。 –

+0

編集を確認すると、本当に閉じるはずです。 – RockyFord

関連する問題