在cakephp中从1个表到另一个表的双重保存

时间:2015-04-16 04:31:46

标签: php cakephp mysqli

我在cakephp中将数据从1个表复制到另一个表。这两张表并不完全相同。问题是我正在复制数据但它每行保存2次。我检查了调试,保存功能导致双重保存。我去了复制的数据,但它在mysql DB(phpmyadmin)中保存了两次

我确信这很简单,但我被困住了。

注意(1024):找不到元素:Elements \ 1.ctp [CORE \ Cake \ View \ View.php,第425行]

public function copystudent(){  
      $this->loadModel('Guardian');
      $this->loadModel('Studentbak'); 
       $students =$this->Studentbak->find('all',array( 
             'conditions' => array('id >=' => 227  ),
              // 'order' => array('guardian_first_name ASC'),
        'recursive'=>-1
              ));

       foreach ($students as $data):
             $this->Student->create();

        $newdata = array();
            $newdata = array(
                'Student' => array(
                      'student_inactive' => $data['Studentbak']['student_inactive'],
                      'student_enq' => $data['Studentbak']['student_enq'],
                    'student_unallocated' => $data['Studentbak']['student_unallocated'],
                      'first_name' => $data['Studentbak']['first_name'],
                    'last_name' => $data['Studentbak']['last_name'],
                      'school' => $data['Studentbak']['school'],
                    'class_year' => $data['Studentbak']['class_year'],
                      'start_date' => $data['Studentbak']['start_date'],
                    'address_street' => $data['Studentbak']['address_street'],
                      'address_suburb' => $data['Studentbak']['address_suburb'],
                    'address_postcode' => $data['Studentbak']['address_postcode'],
                      'address_state' => $data['Studentbak']['address_state'],
                    'address_lat' => $data['Studentbak']['address_lat'],
                      'address_long' => $data['Studentbak']['address_long'],
                    'student_mobile' => $data['Studentbak']['student_mobile'],
                      'additional_information' => $data['Studentbak']['additional_information'],
                    'tutoring_type_id' => $data['Studentbak']['tutoring_type_id'],
                      'referral_id' => $data['Studentbak']['referral_id'],
                        'referral_info' => $data['Studentbak']['referral_info'],
                    'reference_code' => $data['Studentbak']['reference_code'],
                      'tutor_gender_preference' => $data['Studentbak']['tutor_gender_preference'],
                    'created' => $data['Studentbak']['created'],
                      'modified' => $data['Studentbak']['modified'],
                    'address_billing' => $data['Studentbak']['address_billing'],
                      'is_email_notify' => $data['Studentbak']['is_email_notify'],
                    'acknowledge' => $data['Studentbak']['acknowledge'],
                      'user_id' => 0,
                    'guardian_id' => 0,
                      'has_credit' => $data['Studentbak']['has_credit']
                   ));
debug($newdata); 


            if($this->Student->save($newdata, false)) {
                $this->Session->setFlash(__('Data copied'), true);
            }

            endforeach; 
         //   debug($students);  

 }  

1 个答案:

答案 0 :(得分:0)

保存后请写下面的代码

$this->Student->id = null