1
我介紹CakePHP(V2)工作,我想創建關係的形式與3個表:CakePHP的形式與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
);
模型QuizQuestion:
<?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....
}
?>
TasksController家()
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(); ?>
'標題' 和 'Task.QuizQuestion.0.title' 被插入到數據庫,但「任務.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在這裏,但我不知道如何檢索值,任何人都可以幫忙嗎?
讓我知道是否有困難! –
謝謝你的回答! 但有沒有其他方式來檢索數據並將其在數據庫中自動插入?因爲Quizanswer有一個QuizQuestion的外鍵。 – Juck
如果您覺得有用,請投票支持這個答案 –