简体   繁体   English

Cakephp-保存相关模型

[英]Cakephp - Saving related models

I have the following relationships between my models: 我的模型之间具有以下关系:

Assignment hasMany Question
Question belongsTo Assignment
Question hasMany Answer
Answer belongsTo Question

I would like to have a single form that can save the assignment, the questions, and the answers. 我想要一个可以保存作业,问题和答案的表格。

The form is almost working, it saves the assignment information and the questions, but not the answers. 该表格几乎可以使用,它保存作业信息和问题,但不保存答案。

Assignments Controller Create action: 分配控制器创建操作:

function create() {
        if (!empty($this->data)) {

        var_dump($this->data);

        unset($this->Assignment->Question->validate['assignment_id']);
        unset($this->Assignment->Question->Answer->validate['question_id']);
        $this->Assignment->saveAll($this->data, array('validate' => 'first'));
    }
}

create.ctp 创建.ctp

Create new assignment
<?php

echo $this->Form->create('assignment', array('action' => 'create'));
echo $this->Form->input('Assignment.title');
echo $this->Form->input('Assignment.type');
echo $this->Form->input('Question.0.question');
echo $this->Form->input('Question.0.Answer.0.answer');
echo $this->Form->end('Create');

?>

Maybe because: 可能是因为:

Assignment hasMany Question

But Assignment does not have Answer. 但是作业没有答案。

If you find('all') on Assignment with the correct recursive option, it will show all entries on related models. 如果您在Assignment上找到('all')具有正确的递归选项,它将显示相关模型上的所有条目。 But when creating a new assignment it will only save the tables directly related. 但是,在创建新分配时,它将仅保存直接相关的表。 And Answer isn't one. 答案不是一个。

In order to create a new answer, either you make an association between Assignment and Answer or make a function to create the answer after saving the creating Assignment. 为了创建新答案,可以在“分配”和“答案”之间建立关联,或者在保存创建的“分配”后创建函数来创建答案。

You could on assignments_controller.php 你可以在assignments_controller.php上

function create() {
    $this->Assignment->saveAll($this->data, array('validate' => 'first'));
    $this->loadModel('Answer');
    $data[] = array(
        'question_id' => GET_THE_ID_OF_LAST_QUESTION_INSERTED
    );
    $this->Answer->save($data);
}

I'm not sure, but I think you can get the id of last question inserted just using: 我不确定,但是我认为您可以使用以下命令插入最后一个问题的ID:

$this->Assignment->Question->id;

By the way... Why would you want to automatically create a new answer after creating a new assignment/question? 顺便说一句...为什么要在创建新作业/问题后自动创建新答案? It makes no sense. 这个不成立。

I decided to save everything independently. 我决定独立保存所有内容。

What do you think of this possibility? 您如何看待这种可能性?

function create() {

    if (!empty($this->data)) {
        if ($this->Assignment->save($this->data)) {
            foreach($this->data['Question'] as $question) {
                $this->Question->create();
                $question['assignment_id'] = $this->Assignment->getLastInsertId();

                if($this->Question->save($question) && $failed == false) {
                    foreach ($question['Answer'] as $answer) {
                        $this->Answer->create();
                        $answer['question_id'] = $this->Question->getLastInsertId();

                        $this->Answer->save($answer);                           
                    }
                }


            }
        }   
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM