2017-07-14 64 views
0

我正在嘗試創建一個關聯表,其中我已通過一個ExamsQuestions表(它們兩個上都有belongsTo關係)與belongsToMany關係關聯的exam_id和question_id。_joinData在CakePHP 3中沒有正確更新請求

我已經正確創建了(至少一些)關係,因爲我可以正確保存ID,但是我在關聯表中也有字段「questionPoints」和「isActive」字段,但它們不是更新。

我用js文件發送a run-time request.

但是,當我從控制器的功能越來越response,該joindata未設置爲對象正確。

在數據庫中,即使應用了一些._joindata信息(ids),行也不會更新。我認爲這是CakePHP的內置關聯,可以進行連接,因爲默認情況下會應用1級關聯。

在ExamsController中,我首先發送數據到視圖,以正確呈現視圖。之後,我使用請求數據來修補實體並保存實體。

public function take($id) 
{ 
    if ($this->request->is('get')) 
    { 
     $exam = $this->Exams->get($id, [ 
     'contain' => ['ExamTemplates' => ['Questions.Answers']] 
    ]);  
     $users = $this->Exams->Users->find('list', ['limit' => 200]); 
     $examTemplates = $this->Exams->ExamTemplates->find('list', ['limit' => 200]);   
     $this->set(compact('exam', 'users', 'examTemplates')); 
    } 

    if ($this->request->is('post')) { 
     $exam = $this->Exams->get($id, ['contain' => ['Questions']]); 
     $this->autoRender = false; 
     $exam = $this->Exams->patchEntity($exam, $this->request->data); 
     if ($this->Exams->save($exam)) { 
      $response = [ 
       'success' => true, 
       'message' => __("exam updated"), 
       'this' => $this->request->data, 
      ]; 
      $exam_id = $_POST['id']; 
      $this->Flash->success(__('The exam has been updated.')); 
     } else { 

      $response = [ 
       'success' => false, 
       'error' => $exam->errors(), 
       'request' => $this->request->data, 
       'message' => __("Error creating template") 
      ]; 
     } 
     $this->response->body(json_encode($response)); 
    } 
} 

謝謝。

回答

0

好的,我解決了這個問題,但我不知道修復是否以最正確的方式完成。

而是在AJAX調用更新的領域,我改變了_joinData更新基於請求數據控制器發生。

所以,我只是宣佈一個新的_joinData作爲新的數組並把它不會自動添加的字段。有一點需要注意的是,如果我手動將CakePHP自動完成的任何字段賦予給它,它也不會更新。