_joinData没有在Cakephp 3中正确更新请求

时间:2017-07-14 09:18:52

标签: php ajax cakephp controller request

我正在尝试创建一个关联的表,其中我通过一个与两者都具有belongsTo关系的ExamsQuestions表与belongsToMany关系链接的exam_id和question_id。

我已经正确地创建了(至少一些)关系,因为我可以正确地保存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));
    }
}

感谢。

1 个答案:

答案 0 :(得分:0)

好的,我解决了问题,但我不知道修复是否以最正确的方式完成。

我没有更新AJAX调用中的字段,而是根据请求数据将_joinData更新更改为在控制器中发生。

所以我刚刚将新的_joinData声明为新数组,并为其提供了未自动添加的字段。有一点需要注意的是,如果我手动给它任何字段,CakePHP会自动完成,它也不会更新。