[英]CakePHP HABTM data not saving to database
我有一個模型GenForm
,它與另一個模型PdfFile
關系。 我使用它來生成GenForm
索引視圖中的復選框列表。 在GenForm
模型中,我添加了:
public $hasAndBelongsToMany = array(
'PdfFile' => array(
'className' => 'PdfFile',
'joinTable' => 'gen_forms_x_pdf_files'
)
這是我的GenForm
index.ctp
視圖中的一個片段:
<?php
echo $this->Form->input( 'PdfFile', array('label' => 'Select some PDF files', 'multiple' => 'checkbox') );
echo $this->Form->input( 'first_name' );
echo $this->Form->input( 'last_name' );
?>
在控制器中,我有一個基本保存:
if ($this->request->is('post')) { // form was submitted
$this->GenForm->create();
if ($this->GenForm->save($this->request->data)) {
return $this->redirect(array('action' => 'generate', $this->GenForm->id)); // assemble the PDF for this record
} else {
$this->Session->setFlash(__('Log entry not saved.'));
}
}
現在,當我debug()
它時, $this->data
看起來像這樣:
array(
'PdfFile' => array(
'PdfFile' => array(
(int) 0 => '1',
(int) 1 => '5'
)
),
'GenForm' => array(
'first_name' => 'xxx',
'last_name' => 'xxx',
'association_id' => '1',
'email' => ''
)
)
一切都很完美,但我無法驗證復選框(必須至少檢查一個)。 所以,根據這個答案 ,我做了一些改變。
index.ctp
視圖變為:
<?php
echo $this->Form->input( 'GenForm.PdfFile', array('label' => 'Select some PDF files', 'multiple' => 'checkbox') );
echo $this->Form->input( 'first_name' );
echo $this->Form->input( 'last_name' );
?>
這是我的驗證規則:
public $validate = array(
'PdfFile' => array(
'rule' => array(
'multiple', array('min' => 1)
),
'message' => 'Please select one or more PDFs'
)
)
這就是$this->data
現在的樣子:
array(
'GenForm' => array(
'PdfFile' => array(
(int) 0 => '1',
(int) 1 => '5'
),
'first_name' => 'xxx',
'last_name' => 'xxx',
'association_id' => '1',
'email' => ''
)
)
現在PdfFile
的復選框驗證,但PdfFile
數據未保存 - 盡管GenForm
的其他字段已正確保存到自己的表中。
任何人都可以告訴我我缺少什么,以便PdfFile
自動保存並獲得驗證?
陳述顯而易見的,但有效的形式是使用的形式,即:
echo $this->Form->input('PdfFile', array(
'label' => 'Select some PDF files',
'multiple' => 'checkbox'
));
將表單更改為具有名為“PdfFile”的“字段”將無法正常工作 - 因為模型層將刪除不存在的字段的任何數據 - 並且在此表單中它將檢查gen_forms.PdfFile
,發現沒有字段並忽略數據。
要處理驗證錯誤 - 使用在模型上運行的驗證規則來檢查要保存的habtm記錄的數量。 用於驗證的字段的名稱是什么並不重要,例如:
<?php
class GenForm extends AppModel {
public $validate = array(
'dummy' => array( // <- name this whatever you like
'atLeastOne' => array(
'required' => true, // run always
'rule' => array('validateAtLeastOne')
)
)
);
function validateAtLeastOne() {
if (!isset($this->data['PdfFile'])) {
// there is no pdf data at all, ignore this rule
// allow other save operations to work
return true;
}
$return = count(array_filter($this->data['PdfFile']['PdfFile']));
if (!$return) {
$this->PdfFile->invalidate('PdfFile', 'Please upload a file');
}
return $return;
}
}
因為驗證規則如果沒有記錄則返回false,它將停止保存。 通過使用表單助手將查找的相同“字段”名稱調用HABTM關聯上的invalidate,將顯示錯誤消息。
您可以在問題中使用第二種方法:
echo $this->Form->input('GenForm.PdfFile', array(
'label' => 'Select some PDF files',
'multiple' => 'checkbox'
));
完全知道這不是蛋糕期望接收數據然后將其操作為beforeValidate中的正確格式:
<?php
class GenForm extends AppModel {
public $validate = array(
'PdfFile' => array( // existing rule
...
)
);
function beforeValidate() {
if (isset($this->data[$this->alias]['PdfFile'])) {
// keep the existing data as that's what validation will check against
// copy to the right location so Cake will process it
$this->data['PdfFile']['PdfFile'] = $this->data[$this->alias]['PdfFile'];
}
return true;
}
...
}
如果我沒記錯的話,從蛋糕手冊中可能你錯誤地格式化了數據。
試試$this->data
看起來像這樣:
array(
'GenForm' => array(
'first_name' => 'xxx',
'last_name' => 'xxx',
'association_id' => '1',
'email' => ''
),
'PdfFile' => array(
(int) 0 => '1',
(int) 1 => '5'
),
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.