2013-07-22 72 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; 
    } 
} 
相关问题