1

そのため、Excel ファイルからモデルへのデータのアップロードをサポートする動作があります。この Behavior 内から、正常に動作する 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);
}

前もって感謝します。-デイブ

4

1 に答える 1

1

モデルには、レコードをチェックするために呼び出すことができるいくつかのメソッドがあります。Model->validateMany()を使用できるはずです Model クラスには、他にもいくつかの手動検証メソッドがあります。

Model->validateAssociated()は単一のレコードと直接関連付けられたすべてのレコードを検証するために使用され、Model->validates()はすべてのフィールドが検証に合格した場合に true を返します。

Cake Bookの DataValidation セクションを確認してください。Cake 2.2.x では、動的に変化する検証ルールの概念も導入されており、非常に便利です。

乾杯!:)

于 2012-09-19T06:58:43.030 に答える