我陷入了cakephp的循环功能。
逻辑是我需要将用户输入的数据与表中已有的数据进行比较。我有两个表,一个是Bookings
,一个是Inventories_Bookings
。下面是我的编码,但它不起作用。任何帮助!感谢
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 :(得分: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;
}
}