有3个模型的cakepp形式


cakephp form with 3 models

我处理cakepp(v2),我想创建一个与3个表相关的表单:

任务-1,n--1,1-quiz_问题-1,n--1.1-quiz_answers

模型任务:

class Task extends AppModel {
    public $name = 'Task';
    public $hasMany = 'QuizQuestion';
    public $_schema = array(
        'type' => array(
            'type' => 'tinyint'
        ),
        'level' => array(
            'type' => 'tinyint'
        ),
        'date_availability' => array(
            'type' => 'timestamp'
        ),
        'date_end_availability' => array(
            'type' => 'timestamp'
        ),
        'date_accomplishment' => array(
            'type' => 'timestamp'
        ),
        'title' => array(
            'type' => 'string'
        ),
         'description' => array(
            'type' => 'string'
        )
    );
    public $validate = array(
        'type' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'required'   => true,
                'allowEmpty' => false,
            )
        ), ..............etc
    );

模型测验问题:

<?php
App::uses('AppModel', 'Model');
class QuizQuestion extends AppModel {
    public $name = 'QuizQuestion';
    public $hasMany = 'QuizAnswer';
    public $belongsTo = 'Task';
    public $_schema = array(
        'title' => array(
            'type' => 'string'
        )
    );
      etc..
}
?>

型号QuizAnswer

<?php
App::uses('AppModel', 'Model');
    class QuizAnswer extends AppModel {
        public $name = 'QuizAnswer';
        public $belongsTo = 'QuizQuestion' ;
        public $_schema = array(
            'title' => array(
                'type' => 'string'
            )
        );
          ...etc....
    }
    ?>

任务控制器主页()

  public function home() {
            if ($this->request->is('post')) {
                $this->Task->create(false);              
                $this->Task->set($this->request->data);
                $valid = $this->Task->validates();
                /*debug($this->Task);
                exit();*/
                if ($valid) {
                        $r = $this->Task->saveAll($this->request->data['Task']);
                        debug($this->request->data['Task']);
                        if (!$r) {
                            $this->Session->setFlash(
                                    __('INTERNAL_ERROR (%s)', __LINE__), 'flash_error');
                        } else {
                            $this->Session->setFlash(
                                    __('Mission enregistré.'), 'flash_success');
                            $this->redirect(array(
                                'controller' => 'tasks',
                                'action' => 'home'
                            ));
                        }
                }
 else {
                             $this->Session->setFlash( __('pas bon'), 'flash_error');    

 }
            }
    }

形式:

    echo $this->Form->create('Task', $formOptions);
    ?>
    <fieldset>
        <?php
        /*** ETC ***/

        $formLabel['text'] = __('Titre du Quizz');
        echo $this->Form->input('title', array(
            'label' => $formLabel,
            'class' => 'input-xxlarge'
        ));


        $formLabel['text'] = __('Libelle question %u');
            echo $this->Form->input('Task.QuizQuestion.0.title', array(
                'label' => $formLabel,
                'class' => 'input-xxlarge '
            ));
            $formLabel['text'] = __(('0') . ' - ');
            echo $this->Form->input('Task.QuizQuestion.0.QuizAnswer.0.title', array(
                'label' => $formLabel,
                'class' => 'span8'
            ));
        ?>
    </fieldset>


    <div class="form-actions">
<?php
echo $this->Form->button(
        '<i class="icon-white icon-ok"></i> ' .
        __('Publier'), array(
    'escape' => false,
    'class' => 'btn btn-success',
    'div' => null
        )
);
?>
    </div>
        <?php echo $this->Form->end(); ?>

"title"answers"Task.QuizQuestion.0.title"已插入数据库,但"Task.QuizQuestion.0.QuizAnswer.0.title"未插入。

debug($this->request->data['Task']);

array(
    'type' => '1',
    'level' => '1',
    'date_availability' => array(
        'day' => '17',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'date_end_availability' => array(
        'day' => '18',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'date_accomplishment' => array(
        'day' => '18',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'title' => 'ju',
    'description' => 'ji',
    'QuizQuestion' => array(
        (int) 0 => array(
            'title' => 'jo',
            'QuizAnswer' => array(
                (int) 0 => array(
                    'title' => 'je'
                )
            )
        )
    )
)

QuizAnswer在这里,但我不知道如何检索值,有人能帮忙吗?

您可以访问这样的答案

*<?php
$data['Task'] = array(
    'type' => '1',
    'level' => '1',
    'date_availability' => array(
        'day' => '17',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'date_end_availability' => array(
        'day' => '18',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'date_accomplishment' => array(
        'day' => '18',
        'month' => '08',
        'year' => '2012',
        'hour' => '00',
        'min' => '00'
    ),
    'title' => 'ju',
    'description' => 'ji',
    'QuizQuestion' => array(
        (int) 0 => array(
            'title' => 'jo',
            'QuizAnswer' => array(
                (int) 0 => array(
                    'title' => 'je'
                )
            )
        )
    )
)
;
echo $data['Task']['QuizQuestion'][0]['QuizAnswer'][0]['title'];

?>*

你可以把它像这样放在你的代码中,以访问它

$this->request->data['Task']['QuizQuestion'][0]['QuizAnswer'][0]['title'];