2015-03-31 165 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