开发者

Cakephp - Saving related models

开发者 https://www.devze.com 2023-03-24 14:47 出处:网络
I have the following relationships between my models: Assignment hasMany Question Question belongsTo Assignment

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

Create new a开发者_JAVA百科ssignment
<?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. 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

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:

$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);                           
                    }
                }


            }
        }   
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消