2013-07-22 71 views
0

我被困在cakephp的循環函數中。 邏輯是我需要將用戶輸入的數據與已經在表中的數據進行比較。我有兩張桌子,一張是Bookings,另一張是Inventories_Bookings。下面是我的編碼,但它不工作。任何幫助!謝謝與數據庫中的數據輸入的匹配數據CAKEPHP

public function add2() { 
    if ($this->request->is('post')) { 
     foreach ($invbook as $invenbook) 
     { 
      if ($this->request->data['Booking']['bookings_location'] == $invenbook['InventoriesBooking']['test']) 
      { 
       $this->Session->setFlash(__('The booking cannot be created')); 
       $this->redirect(array('action' => 'add2')); 
       debug($this->request->data['Booking']['bookings_location'] == $invenbook['InventoriesBooking']['test']); 
      } 
     } 

     $this->Booking->create(); 
     $invbook = $this->Booking->InventoriesBooking->find('list',array('fields' => array('InventoriesBooking.id', 'InventoriesBooking.test'))); 
     $this->set(compact('invbook')); 
    } 
} 
+0

「不起作用」是什麼意思?你有任何錯誤? – dhofstet

+0

第0步:確保您的調試模式是否打開..如果不確定的地方配置::寫('調試',2);在你的add2函數中。 步驟1:調試用戶輸入的數據,在你的情況下,它可能類似debug($ this-> request-> data); 第2步:調試已經在表中的數據..我猜可能是類似調試($ invbook); 第3步:檢查您的比較$ this-> request-> data ['Booking'] ['bookings_location'] == $ invenbook ['InventoriesBooking'] ['test']是否正確。步驟4:反饋您的測試結果,以便我們知道發生了什麼。 –

回答

0

我會使用自定義驗證功能。

您可以在模型中創建自己的函數,從這裏您可以訪問數據庫來執行查找。如果它匹配,你可以返回true。

You can read about custom validation methods in the book.

There is an example of a rule like this using the db in the book. 引用偉大的正義。

class User extends AppModel { 

    public $validate = array(
     'promotion_code' => array(
      'rule' => array('limitDuplicates', 25), 
      'message' => 'This code has been used too many times.' 
     ) 
    ); 

    public function limitDuplicates($check, $limit) { 
     // $check will have value: array('promotion_code' => 'some-value') 
     // $limit will have value: 25 
     $existing_promo_count = $this->find('count', array(
      'conditions' => $check, 
      'recursive' => -1 
     )); 
     return $existing_promo_count < $limit; 
    } 
} 
相關問題