2015-03-31 163 views
0

嗨我試着去創造CakePHP中的高級搜索形式,CakePHP的自定義搜索

形式包括在下拉框,複選框(HABTM關係),文本字段,以及價格遊俠(滑塊), 你可以看到下面的表格:

enter image description here

正如你可以看到我有多個下拉菜單,複選框多等

搜索的偉大工程,但我想是我該如何節省網址這些改變或過濾器FO用戶之後的R例如已經選擇了幾個過濾器如下:

enter image description here

我想要的網址是如下: http://localhost/website/cakephpapplication/search?location=something&room_type=sth&accommodation_type=something& .... 等一個

,我用行動是這一個:

////////////////////////////////////////////////////////////////////////////////////  
/** 
* view method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function search() { 
     $this->layout = 'frontend_search'; 
     $default_lang_code = Configure::read('Config.language'); 
     if($this->Session->check('Config.language')) { // Check for existing language session 
      $language_code = $this->Session->read('Config.language'); // Read existing language 
      $languageData = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$language_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1)); 
      if (!empty($languageData)) { 
       $language_id = $languageData['Language']['id']; 
      } else { 
       $language_id = $this->Language->findLanguageIdByCode($default_lang_code); 
      } 
     } else { 
      $language_code = $default_lang_code; // Read existing language 
      $languageData = $this->Language->find('first', array('conditions' => array('Language.language_code'=>$default_lang_code),'recursive' => -1,'fields' => array('Language.id'),'limit' => 1)); 
      $language_id = $languageData['Language']['id']; 
     } 

     $localeCurrency = 'EUR';   
     if ($this->Session->check('LocaleCurrency')) { 
      $localeCurrency = $this->Session->read('LocaleCurrency'); 
     } 

     $currencies = $this->Currency->find('all', array('recursive'=>-1)); 

     if ($this->request->is('post')) { 
      if(!empty($this->request->data)){ 
       /*THIS IS THE MOS REQUESTED PROPERTIES */ 
       $searchQuery =" SELECT Property.* , PropertyTranslation.*, Wishlist.*, Currency.*"; 
       $searchQuery .=" FROM properties as Property"; 
       $searchQuery .=" JOIN property_translations as PropertyTranslation"; 
       $searchQuery .=" ON Property.id = PropertyTranslation.property_id"; 
       $searchQuery .=" JOIN users as User"; 
       $searchQuery .=" ON User.id = Property.user_id"; 

       $searchQuery .=" LEFT JOIN wishlists as Wishlist"; 
       $searchQuery .=" ON Wishlist.property_id = Property.id "; 
       $searchQuery .=" LEFT JOIN currencies as Currency"; 
       $searchQuery .=" On Currency.id = Property.currency_id"; 

       if (isset($this->request->data['Safety']) && !empty($this->request->data['Safety']) && $this->request->data['Safety']!='') { 
        $searchQuery .=" JOIN properties_safeties as PropertiesSafety"; 
        $searchQuery .=" ON Property.id = PropertiesSafety.property_id"; 
        foreach ($this->request->data['Safety'] as $key => $value) { 
         $searchQuery .= " AND PropertiesSafety.safety_id = '".$value."'"; 
        } 
       } 

       if (isset($this->request->data['Extra']) && !empty($this->request->data['Extra']) && $this->request->data['Extra']!='') { 
        $searchQuery .=" JOIN extras_properties as ExtrasProperty"; 
        $searchQuery .=" ON Property.id = ExtrasProperty.property_id"; 
        foreach ($this->request->data['Extra'] as $key => $value) { 
         $searchQuery .= " AND ExtrasProperty.extra_id = '".$value."'"; 
        } 
       } 

       if (isset($this->request->data['Service']) && !empty($this->request->data['Service']) && $this->request->data['Service']!='') { 
        $searchQuery .=" JOIN properties_services as PropertiesService"; 
        $searchQuery .=" ON Property.id = PropertiesService.property_id"; 
        foreach ($this->request->data['Service'] as $key => $value) { 
         $searchQuery .= " AND PropertiesService.service_id = '".$value."'"; 
        } 
       } 

       if (isset($this->request->data['Characteristic']) && !empty($this->request->data['Characteristic']) && $this->request->data['Characteristic']!='') { 
        $searchQuery .=" JOIN characteristics_properties as CharacteristicsProperty"; 
        $searchQuery .=" ON Property.id = CharacteristicsProperty.property_id"; 
        foreach ($this->request->data['Characteristic'] as $key => $value) { 
         $searchQuery .= " AND CharacteristicsProperty.characteristic_id = '".$value."'"; 
        } 
       } 

       $searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0 AND PropertyTranslation.language_id='".$language_id."' "; 
       if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') { 
         $searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'"; 
       } 

       if (isset($this->request->data['Property']['address']) && !empty($this->request->data['Property']['address']) && trim($this->request->data['Property']['address'])!='') { 
        $searchQuery .= " AND Property.address LIKE '%".$this->request->data['Property']['address']."%' "; 
       } 

       if (isset($this->request->data['Property']['capacity']) && !empty($this->request->data['Property']['capacity']) && $this->request->data['Property']['capacity']!='') { 
        $searchQuery .= " AND Property.capacity ='".$this->request->data['Property']['capacity']."'"; 
       } 

       if (isset($this->request->data['Property']['room_number']) && !empty($this->request->data['Property']['room_number']) && $this->request->data['Property']['room_number']!='') { 
        $searchQuery .= " AND Property.room_number ='".$this->request->data['Property']['room_number']."'"; 
       } 

       if (isset($this->request->data['Property']['bed_number']) && !empty($this->request->data['Property']['bed_number']) && $this->request->data['Property']['bed_number']!='') { 
        $searchQuery .= " AND Property.bed_number ='".$this->request->data['Property']['bed_number']."'"; 
       } 

       if (isset($this->request->data['Property']['bathroom_number']) && !empty($this->request->data['Property']['bathroom_number']) && $this->request->data['Property']['bathroom_number']!='') { 
        $searchQuery .= " AND Property.bathroom_number ='".$this->request->data['Property']['bathroom_number']."'"; 
       } 

       if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && trim($this->request->data['Property']['property_type'])!='') { 
        $searchQuery .= " AND Property.property_type LIKE '%".$this->request->data['Property']['property_type']."%'"; 
       } 

       if (isset($this->request->data['Property']['property_type'])!='' && !empty($this->request->data['Property']['property_type']) && $this->request->data['Property']['property_type']=='sale') { 

        if (isset($this->request->data['Property']['surface_area'])!='' && !empty($this->request->data['Property']['surface_area'])) { 
         $searchQuery .= " AND Property.surface_area = '".$this->request->data['Property']['surface_area']."'"; 
        } 

        if (isset($this->request->data['Property']['sale_price'])!='' && !empty($this->request->data['Property']['sale_price'])) { 
         $searchQuery .= " AND Property.sale_price = '".$this->request->data['Property']['sale_price']."'"; 
        } 


       } 

       if (isset($this->request->data['Property']['room_type_id']) && !empty($this->request->data['Property']['room_type_id']) && $this->request->data['Property']['room_type_id']!='') { 
        $searchQuery .= " AND Property.room_type_id = '".$this->request->data['Property']['room_type_id']."'"; 
       } 

       if (isset($this->request->data['Property']['accommodation_type_id']) && !empty($this->request->data['Property']['accommodation_type_id']) && $this->request->data['Property']['accommodation_type_id']!='') { 
        $searchQuery .= " AND Property.accommodation_type_id = '".$this->request->data['Property']['accommodation_type_id']."'"; 
       } 

       if (isset($this->request->data['Property']['price']) && !empty($this->request->data['Property']['price']) && $this->request->data['Property']['price']!='') { 
        $price = $this->request->data['Property']['price']; 
        list($minPrice, $maxPrice) = explode(";", $price); 
        $searchQuery .= " AND Property.rent_daily_price BETWEEN '".$minPrice."' AND '".$maxPrice."'"; 
       } 

       $searchQuery .=" ORDER BY Property.id"; 
       $searchResults = $this->Property->query($searchQuery); 
      } 
     } else { 
      $searchQuery =" SELECT Property.*, PropertyTranslation.*, Wishlist.*, Currency.* "; 
      $searchQuery .=" FROM properties as Property"; 
      $searchQuery .=" JOIN property_translations as PropertyTranslation"; 
      $searchQuery .=" ON Property.id = PropertyTranslation.property_id"; 
      $searchQuery .=" JOIN users as User"; 
      $searchQuery .=" ON User.id = Property.user_id"; 

      $searchQuery .=" LEFT JOIN wishlists as Wishlist"; 
      $searchQuery .=" ON Wishlist.property_id = Property.id "; 

      $searchQuery .=" LEFT JOIN currencies as Currency"; 
      $searchQuery .=" On Currency.id = Property.currency_id"; 

      $searchQuery .=" WHERE Property.property_status=1 AND Property.publish_status=1 AND User.is_first_post=0 AND PropertyTranslation.language_id='".$language_id."'"; 
      $searchQuery .=" ORDER BY Property.id"; 
      $searchResults = $this->Property->query($searchQuery); 
     } 

     $this->set(compact('searchResults','roomTypes','accommodationTypes','safeties','extras','services','characteristics','currencies')); 
    } 
////////////////////////////////////////////////////////////////////////////////////  

我真的很感謝你的幫助。 在此先感謝。

回答

1

搜索的偉大工程,但我想是我該如何節省網址這些變化或過濾器,例如,用戶已經選擇了幾個過濾器如下之後:

希望實現the PRG pattern那將POST變成GET。

自己做,維基文章解釋了模式,或使用現有的插件:

+0

@buruzum我很抱歉,但我不明白,怎麼我可以將這些插件實施到我的應用程序嗎? – Johnny 2015-03-31 09:42:46

+0

閱讀提供的文檔和CakePHP手冊,瞭解如何使用插件?這兩個插件都帶有文檔和代碼示例。 – burzum 2015-03-31 09:48:30

+0

thanx爲您的重播,我試過這個插件,但沒有exacly我想要的,所以我設法解決它自己在一個工作。 :)) – Johnny 2015-04-01 07:55:47