정확하게 이해하면 각 양식의 데이터베이스에 고유 한 식별자가 없습니다. 이 경우,이를 식별 할 수있는 방법이 필요합니다. 여기서 간단한 뷰 파일 이름 배열을 사용 하겠지만 뷰 이름을 필드로 사용하고 제출 된 응답을 질문에 연결하는 것이 더 나을 것입니다. 컨트롤러에서
// always make sure that the current view gets passed as a parameter
echo $form->create('Question', array('url'=>array('action'=>'next', $currentView));
...
echo $form->submit('Next');
:
var $questionViews = array('q1', 'q2', 'q3'); // filenames of the views
function next($view = null) {
if(isset($this->data)) {
// save answers to database, session, etc
if(/*save successful*/) {
// redirect to the same action, which will load a new view because
// no form data was passed
$this->redirect(array('action'=>'next'));
exit();
}
}
$toRender = null; // view to be rendered
if($view && in_array($view, $this->questionViews)) {
// render the explicitly passed view
$toRender = $view;
} else {
// grab a random view
$toRender = $this->questionViews[array_rand($this->questionViews)];
}
$this->set('currentView', $toRender); // used by form action
$this->render($toRender); // explicit call to render stops the default view from being rendered
}