0
votes

I have a simple HABTM with a lessons/Students table. i simply create a new lesson with an existing student via a while loop , so I should get 2 new rows. I am testing this function. What I get is 4 new rows in the lesson table instead of 2. I have no idea why the extra 2 rows are created as they are a duplicates of the 2 new rows which are outputted in debug. Everything is correctly saved like the entries in join lesson/student table and all the FK exist.

Just to add confusion, sometimes the same code produces the desired 2 rows . This is unstable so I am doing something wrong. I followed the array setup for habtm as in the manual for saves.

http://book.cakephp.org/2.0/en/models/saving-your-data.html

 private function book_lessons($lesson=null) {

       // debug( $lesson);

     $i=0;
     while ($i<2)
     {
      $date=date('Y-m-d');
      $data[$i]=array();
      $this->Lesson->create();

      $data[$i]['Lesson']['lesson_date']= $date;
      $data[$i]['Lesson']['start_time']= $lesson['Lesson']['start_time'];
      $data[$i]['Lesson']['end_time']=$lesson['Lesson']['end_time'];

      $data[$i]['Lesson']['schedule_rec']= 1;
      $data[$i]['Lesson']['subject_id']= $lesson['Lesson']['subject_id'];
      $data[$i]['Lesson']['tutoring_type_id']= 1;
      $data[$i]['Lesson']['tutor_id']= $lesson['Lesson']['tutor_id'];
      $data[$i]['Lesson']['subject_id']= $lesson['Lesson']['subject_id'];
      $data[$i]['Lesson']['term_id']= $lesson['Lesson']['term_id'];

      $data[$i]['Student']['id']=$lesson['Student']['id'];

     $i=$i+1;

     }


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

public $hasAndBelongsToMany = array(

        'Student' => array(
            'className' => 'Student',
            'joinTable' => 'lessons_students',
            'foreignKey' => 'lesson_id',
            'associationForeignKey' => 'student_id',
            'unique' => 'keepExisting',

        )
    );


array(
    (int) 0 => array(
        'Lesson' => array(
            'lesson_date' => '2015-06-11',
            'start_time' => '16:00:00',
            'end_time' => '17:00:00',
            'schedule_rec' => (int) 1,
            'subject_id' => '16',
            'tutoring_type_id' => (int) 1,
            'tutor_id' => '12',
            'term_id' => '10'
        ),
        'Student' => array(
            'id' => '206'
        )
    ),
    (int) 1 => array(
        'Lesson' => array(
            'lesson_date' => '2015-06-11',
            'start_time' => '16:00:00',
            'end_time' => '17:00:00',
            'schedule_rec' => (int) 1,
            'subject_id' => '16',
            'tutoring_type_id' => (int) 1,
            'tutor_id' => '12',
            'term_id' => '10'
        ),
        'Student' => array(
            'id' => '206'
        )
    )
)
1
I still cant fix this issue as I get a duplicate row for every row added? - ajt

1 Answers

0
votes

Move the $this->Lesson->create(); out of the loop. Cake will still save multiple records when you have only in create() method.