cakephp使用saveMany保存许多数组

时间:2015-01-18 17:11:25

标签: arrays cakephp

使用这些示例两个数组:

array(
(int) 0 => array(
    'admission_no' => '1003',
    'exam_id' => '1',
    'subject_code' => (int) 101,
    'score' => '61'
),
(int) 1 => array(
    'admission_no' => '1003',
    'exam_id' => '1',
    'subject_code' => (int) 102,
    'score' => '85'
)

array(
(int) 0 => array(
    'admission_no' => '1005',
    'exam_id' => '1',
    'subject_code' => (int) 101,
    'score' => '63'
),
(int) 1 => array(
    'admission_no' => '1005',
    'exam_id' => '1',
    'subject_code' => (int) 102,
    'score' => '14'
)

我已经设法将它们放入saveMany()所需的格式中。我尝试过foreach循环,但只设法保存第一个数组。它可能很简单,但由于某种原因,我的循环没有按预期工作

另外..值得一提的是,上面的数组是对csv文件进行一些操作的结果。我以前得到的代码在这里:

public function combineElements ($arr) {
    $columns = array('admission_no','exam_id','subject_code','score');
    $cust = array_combine($columns,$arr);
    return $cust;
    //debug($cust);
}

public function resultsUpload () {
    $modelClass = $this->modelClass;

    //populate the $data variable using CsvComponent
    $data = $this->Csv->import(($this->request->data[$modelClass]['result']['tmp_name']));
    //get the exam category passed from the $_POST variable
    $exam_cat = $this->request->data[$modelClass]['exam_id'];
    //debug($data);

    //$columns = array('admission_no','exam_id','subject_code','score');
    //manipulate $data array to get values as desired
    foreach ($data as $value) {
         //remove the first element of $value and retain the indexes to retrieve subjects and scores
         $new_sliced = array_slice($value, 1,(sizeof($value) - 1),true);
        $subject = array_keys($new_sliced);
        $scores = array_values($new_sliced);
        $number = array_shift($value);
         $result_data = array();
        for ($i = 0; $i < count($subject); $i++) {
        $result_data[] = $number;
        $result_data[] = $exam_cat;
        $result_data[] = $subject[$i];
        $result_data[] = $scores[$i];
        }
        //debug($result_data);
       $customized = array_chunk($result_data,4);
      //debug($customized);
      $data_set = array_map(array($this, 'combineElements'),$customized);
      debug($data_set);
       $this->$modelClass->create();

     if($this->$modelClass->saveMany($k, array(
        'validate' => false, 'atomic' => true
      ))){
            $this->Session->setFlash(__('Results uploaded successfully'));
            $this->redirect(array('action' => 'index'));
      }else {
        $this->Session->setFlash(__('Could not upload results.Check for errors then try again'));
        $this->redirect(array('action' => 'resultsUpload'));
      }
    }
    }
    $exams = $this->ExamsScore->Exam->find('list',array('fields' => array('exam_type')));
     $this->set('modelClass',$modelClass);
     $this->set('exams',$exams);
     $this->render('../Common/upload');
    // debug($exams);
}

以上数组是debug($ data_set)

的结果

1 个答案:

答案 0 :(得分:0)

尝试使用SaveAll ..

 if($this->$modelClass->saveAll($data, array(
    'validate' => false, 'atomic' => true
  ))){}

还有一件事是$ k代表saveMany函数。

编辑..

确保在saveMany或saveAll中传递您的数组,如:

array(
    0 => array(
            'MyModel' => array(
                'admission_no' => '1005',
                'exam_id' => '1',
                'subject_code' => (int) 101,
                'score' => '63'
            )
        ),
    1 => array(
        'MyModal' => array(
            'admission_no' => '1005',
            'exam_id' => '1',
            'subject_code' => (int) 102,
            'score' => '14'
        )
    )
);