0
votes

In CakePHP when I save the data with the below code, I get the data being saved as expected but I get all the data saved 2 times. The $lesson data variable does not retrieve 2 copies of the data so the save function is the problem. I tried using save in a loop and that gives the same problem. There must be something simple I am doing wrong.

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

$data=array();
$i=0;

$this->Lessondata->cacheQueries = false;
$this->Lessondata->create( );

foreach ($lessons as $item):
    //  $this->Lessondata->cacheQueries = false; 

    $data[$i]['Lessondata']['lesson_id']=$item['Lesson']['id'];
    $data[$i]['Lessondata']['st_id']=$item['Student']['id'];
    $data[$i]['Lessondata']['tutor_id']=$item['Tutor']['id'];
    $data[$i]['Lessondata']['student_name']=$item['Student']['first_name'].' '.$item['Student']['last_name'];
    $data[$i]['Lessondata']['subject']=$item['Subject']['name'];
    $data[$i]['Lessondata']['tutor_name']=$item['Tutor']['first_name'].' '.$item['Tutor']['last_name'];
    $data[$i]['Lessondata']['class_year']=$item['Student']['class_year'];
    //  debug($data[$i]);
    debug($i);
    $i=$i+1;

endforeach;

$this->Lessondata->saveAll($data);
2
try $data['Lessondata'][$i]['lesson_id'] etcSalines
i did this but it doesnt save, how do I set up the save ? Why did the original save duplicate as I am not understanding what cakephp is doing with saving 2 timesajt
The primary key of 'Lessondata' is 'lesson_id', maybe?Igor Greg
no there primary is not set as it is a new recordajt

2 Answers

0
votes

Try it.

$data = array();
    foreach ($lessons as $item):
        $data['Lessondata'][] = array(
            'lesson_id' => $item['Lesson']['id'],
            'st_id' => $item['Student']['id'],
            'tutor_id' => $item['Tutor']['id'],
            'student_name' => $item['Student']['first_name'] . ' ' . $item['Student']['last_name'],
            'subject' => $item['Subject']['name'],
            'tutor_name' => $item['Tutor']['first_name'] . ' ' . $item['Tutor']['last_name'],
            'class_year' => $item['Student']['class_year'],
        );

    endforeach;

    $this->Lessondata->create();
    $this->Lessondata->saveAll($data);
0
votes

This works but I need to check when the duplication starts. This is a bad answer as I am allowing for a quirk. As yet there is no other answer. I am happy to take this down if someone provides a working answer.

$this->Lessondata->create();

foreach ($lessons as $item):
  $ff = $this->Lessondata->find('first', array(
    'conditions' => array('Lessondata.lesson_id' => $item['Lesson']['id'])
  ));

  if (!empty($ff)) {
    debug($item);
    break;
  }

  $data[$i]['Lessondata']['lesson_id'] = $item['Lesson']['id'];
  $data[$i]['Lessondata']['st_id'] = $item['Student']['id'];
  $data[$i]['Lessondata']['tutor_id'] = $item['Tutor']['id'];
  $data[$i]['Lessondata']['student_name'] = $item['Student']['first_name'].' '.$item['Student']['last_name'];
  $data[$i]['Lessondata']['subject'] = $item['Subject']['name'];
  $data[$i]['Lessondata']['tutor_name'] = $item['Tutor']['first_name'].' '.$item['Tutor']['last_name'];
  $data[$i]['Lessondata']['class_year'] = $item['Student']['class_year'];    
  $i = $i+1;
endforeach;
debug($data);

$this->Lessondata->saveAll($data);