2016-03-03 138 views
0

我在表中分頁結果。現在添加過濾數據的方式,其中一種過濾方式是創建日期,用戶可以輸入日期「from」以及日期「to」。我認爲我這樣做的方式失敗了,當我得到一個後期verficiando使用數據類型的用戶來過濾。阿爾莫與條件和領域的安排,過濾並最終返回到頁面。我離開我的代碼條件和過濾器CakePHP

if ($this->request->is('post')) { 
      if(isset($this->request->data['LogsAccione']['user_id'])){ 
       $options['conditions']['LogsAccione.user_id'] = $this->request->data['LogsAccione']['user_id']; 
      } 
      if (isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from']) && isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])) { 
       $options['conditions']['LogsAccione.created'] = array(
        '>=' => date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from'])), 
        '<=' => date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to'])) 
       ); 
      } 
} 

很明顯,這個失敗,但無法找到如何以我想要的方式做到這一點。創建一個具有多個選項的排列,以便在篩選後顯示給用戶。在用戶的特定情況下,它的工作原理是因爲只有一個選項,並且這是相同的,但在創建日期的情況下是兩種可能性之間。

回答

1

解決方案:

if ($this->request->is('post')) { 
     if(isset($this->request->data['LogsAccione']['user_id']) && !empty($this->request->data['LogsAccione']['user_id'])){ 
      $options['conditions'][] = "LogsAccione.user_id ='".$this->request->data['LogsAccione']['user_id']."'"; 
     } 
     if (isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from']) && isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])) { 
      $options['conditions'][] = "LogsAccione.created >='".date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from']))."'"; 
      $options['conditions'][] = "LogsAccione.created <='".date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to']))."'"; 
     }elseif(isset($this->request->data['LogsAccione']['created_from']) && !empty($this->request->data['LogsAccione']['created_from'])){ 
      $options['conditions'][] = "LogsAccione.created >='".date('Y-m-d 00:00:00', strtotime($this->request->data['LogsAccione']['created_from']))."'"; 
     }elseif(isset($this->request->data['LogsAccione']['created_to']) && !empty($this->request->data['LogsAccione']['created_to'])){ 
      $options['conditions'][] = "LogsAccione.created <='".date('Y-m-d 23:59:59', strtotime($this->request->data['LogsAccione']['created_to']))."'"; 
     } 
    } 
+1

而不是去爲醜陋的和重複的解決方案檢查此插件。 https://github.com/FriendsOfCake/search它是處理過濾參數和條件。 – burzum