Wednesday, June 10, 2015

saveall on habtm creates duplicate new rows

Hi, 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();
         
         
          $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'
            )
        )
    )

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: