2016-09-23 5 views
1

cakephp3에 관련 레코드를 저장할 수 있지만 코드를 보면 레코드가 한 번의 호출로 한 번에 저장되지 않습니다. 한 번에 모든 레코드를 저장하는 동안 문제가 발생했습니다. Guardian과 Users 테이블이 어떻게 보입니까? 코드가 작동하고 레코드를 저장하지만 가디언 테이블에 오류가 발생하여 1 회 저장 호출에서 모두를 저장하는 것이 문제가되었습니다.관련된 모든 레코드를 한 번에 저장

Guardians have a 1 to many relationship with students 
Students and Users have a 1 to 1 
Students to subjects and availabilityFotStudents both have a many to many 

코드

public function add($gId=0) { 
     $this->loadModel("Guardians"); 
     $this->loadModel('PaymentTypes'); 
     $this->set("title","Add Student"); 
     $guardians=null; 


     if ($gId>0){ 
      $guardians =$this->Guardians->get($gId); 
     } 

     if ($this->request->is('post')) { 

      if ($this->request->data['studenttype']==0){ //enquiry 
       $this->request->data['students']['address_billing'] = 0; 
       $this->request->data['students']['student_enq'] = 1; 
      } 
      else if ($this->request->data['studenttype']==1){ //waitlist 
       $this->request->data['students']['address_billing'] = 1; 
       $this->request->data['students']['student_enq'] = 0; 
      } 
      else if ($this->request->data['studenttype']==2){ //skip waitlist 
       $this->request->data['students']['address_billing'] = 4; 
       $this->request->data['students']['student_enq'] = 0; 
      }    

      if ($this->request->data['students']['tutoring_typest_id']==1){ 
       $this->request->data['students']['group_status']=0; 
      } 
      else if ($this->request->data['students']['tutoring_typest_id']>1){ 
       $this->request->data['students']['group_status']=1; 
      } 

      if ($this->request->data['students']['tutoring_typest_id']==3){//group only 
       $this->request->data['students']['address_billing'] = 4; 
      } 

      $data = $this->request->data; 
      // debug($data); 



       $uname= $this->request->data['Users']['username']; 
       if ($this->Students->Users->findByUsername($uname)->count()) { 
       $this->Flash->error(__('Username exists. Please, try again.')); 
       return $this->redirect(["action"=>"add",$gId]); 
       } 
       $userId=0; 

       $entity = $this->Students->Users->newEntity($this->request->data['Users'],['validate'=>false]); 
       $entity->role = "student"; 
      $entity['role_id'] = 4; 
       $entity = $this->Students->Users->save($entity); 

      // debug($entity); 
       $studentUserId = $entity->id; 


      if($guardians==null) { 
       $guardians = $this->Guardians->newEntity($this->request->data['guardians'], ['validate' => false]); 
      } 

      $guardianEntity = $this->Guardians->save($guardians); 
      $guardians = $this->Students->newEntity(); 
      $studentData = $this->request->data['students']; 
      $studentData['subjects'] = $this->request->data['subjects']; 
      $studentData['availability_for_students'] = $this->request->data['availability_for_students']; 
      $studentEntity = $this->Students->patchEntity($guardians,$studentData, 
       [ 
        "validate"=>false, 
        'associated' => [ 
         "AvailabilityForStudents"=>["validate"=>false], 
         "Subjects"=>["validate"=>false] 
        ] 

       ] 
      ); 
      $studentEntity->guardian_id = $guardianEntity->id; 
      $studentEntity->user_id = $studentUserId; 

      $studentEntity = $this->Students->save($studentEntity, 
       [ 
        "validate"=>false, 
        'associated' => [ 
         "AvailabilityForStudents"=>["validate"=>false], 
         "Subjects"=>["validate"=>false] 
        ] 

       ] 
      ); 
      if ($studentEntity) { 
       $this->Flash->success(__('The student has been saved')); 
       return $this->redirect(["action"=>"index2"]); 
      } else { 
       $this->Flash->error(__('The student could not be saved. Please, try again.'),'flash_alert'); 
      } 

     }//post 
     $subjects = $this->Students->Subjects->find('list', array('order' => array('Subjects.name' => 'asc'))); 
     $weekdays = $this->Students->weekDays; 
     $tutoringTypes = $this->Students->TutoringTypeStudents->find('list'); 


     //$payments = $this->Student->paymentOptions; 
     $this->PaymentTypes->primaryKey("name"); 
     $payments = $this->PaymentTypes->find('list', array(
      'fields'  => array('PaymentTypes.name','PaymentTypes.name')) ); 
     $referrals = $this->Students->Referrals->find('list'); 
     $tutorGender = $this->Students->Lessons->Tutors->tutorGender; 

     $this->set('guardians', $guardians); 
     $this->set(compact('referrals','subjects', 'tutoringTypes', 'weekdays','payments','tutorGender')); 

    } 
+0

누구나 아이디어가 있다면 여전히 작동하지 않습니다. – ajt

답변

3

당신은 Tranactional 데이터를 저장할 수 있습니다. 어디에서 여러 저장 방법을 실행할 수 있습니다. 저장 메소드가 실패하면 저장 될 데이터가 없습니다. 그래서 당신이 언급 한 "1 save call"처럼 작동합니다.

관련 문제