所以,我有一個行爲,支持從Excel文件上傳數據到模型。從這個行爲中,我調用saveMany,它工作正常。但是,如果數據無效,我不會收到任何錯誤,只會發生無聲失敗(我在此例中返回一個通用錯誤,但我希望驗證失敗的細節)。CakePHP沒有從saveMany獲得驗證消息(從行爲調用)
我敢肯定,有一種方法可以解決這個問題,我作爲CakePHP的新手,不知道。這裏是行爲的主要功能(這裏有額外的邏輯的一堆可能不是有密切關係的問題,但我不願意的情況下宰殺它原來是巧妙地相關):
public function parseExcelFile(Model &$model, $fileInfo, $lookupKeys, $otherData = null, $referencedFields = null) {
$allowedTypes = array('application/vnd.ms-excel');
if ((isset($fileInfo['error']) && $fileInfo['error'] == 0) ||
(!empty($fileInfo['tmp_name']) && $fileInfo['tmp_name'] != 'none')) {
// basic pre-checks are done. Now ask PHP if it's a good file
if (is_uploaded_file($fileInfo['tmp_name'])) {
// it's a good uploaded file. Now check the type
if (in_array($fileInfo['type'], $allowedTypes)) {
$data = new Spreadsheet_Excel_Reader($fileInfo['tmp_name']);
// Check text of header row to be sure that it hasn't been changed (equal to db field name)
$numAltered = 0;
$numAdded = 0;
$schema = $model->schema();
$newModelData = array($model->name => array()); // this is a holder for the data to be added to the model
$fieldlist = array(); // this will be an array of field names found in the Excel file, and which we have data for
$newData = $this->modifyDataForReferencedFields($model, $data, $referencedFields); // $newData is now 0-based.
for ($curCol = 0; $curCol < count($newData[0]); $curCol++) {
$curFieldName = $newData[0][$curCol];
if (!array_key_exists($curFieldName, $schema)) {
return 'Row header "'.$curFieldName.'" is not one of the database fields.'
.' You probably altered the template in an incorrect manner.';
} else {
// set up the fieldlist and newModelData arrays
array_push($fieldlist, $curFieldName);
$newModelData[$model->name][$curFieldName] = null;
}
}
// append $otherData fields to fieldlist
foreach ($otherData as $key => $value) {
array_push($fieldlist, $key);
}
// So, the headers seem okay, let's try to enter the data into the Model
$saveData = array();
for ($curRow = 1; $curRow < count($newData); $curRow++) { // row 0 is the headers
// put the data into the newModelData
for ($curCol = 0; $curCol < count($newData[0]); $curCol++) {
$curFieldName = $newData[0][$curCol];
$curVal = $newData[$curRow][$curCol];
$newModelData[$model->name][$curFieldName] = $curVal;
}
$existingID = $this->existingID($model, $lookupKeys, $newModelData[$model->name]);
if ($existingID) {
// we must be updating a model entry, so set the ID
$newModelData[$model->name]['id'] = $existingID;
$numAltered++;
} else {
// otherwise, unset
unset($newModelData[$model->name]['id']);
$numAdded++;
}
// Add in the fixed fields
foreach ($otherData as $key => $value) {
$newModelData[$model->name][$key] = $value;
}
array_push($saveData, $newModelData);
}
$options = array('fieldlist' => $fieldlist);
if ($model->saveMany($saveData, $options)) {
return 'From the uploaded file, '.$numAdded.' records were added and '.$numAltered.' records were updated.';
} else {
return 'There was a problem with the uploaded data.';
}
} else {
return "The chosen file was not one of the allowed types.";
}
} else {
return "There was something wrong with the upload process.";
}
} else {
return "No file was chosen for uploading.";
}
}
而且這裏是上傳表單提交時調用的控制器動作:
public function processUpload() {
// now parse the file in the model
$result = $this->Instructor->parseExcelFile(
$this->data['Instructor']['bulkData'],
array( // field(s) for looking up data in the model to see if we are adding or updating
'username',
),
array( // a set of fixed fields to add to all entries, new or updates
'department_id' => $this->request->data['Instructor']['department_id'],
'role' => 'instructor',
'password' => '')
);
$this->Session->setFlash($result);
$this->redirect($this->referer(), null, true);
}
在此先感謝。 -Dave
但在保存過程中數據是否已經過驗證?其實,我知道他們是因爲無效數據無法保存。這不是驗證,而是我錯過的閃光消息。或者我可能誤解了一些東西。 –
那麼在保存期間應該調用驗證,但是可能是因爲您沒有以任何方式與視圖進行通信,而是調用'saveMany()'。 –