我在 cakephp (v2) 上工作,我想创建一个与 3 个表相关的表单:
任务--1,n--1,1--quiz_questions--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..
}
?>
模型测验答案
<?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' 和 'Task.QuizQuestion.0.title' 被插入到数据库中,但 'Task.QuizQuestion.0.QuizAnswer.0.title' 没有。
调试($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 在这里,但我不知道如何检索值,有人可以帮忙吗?