学生有许多付款和付款属于学生。创建付款时,我必须指明我为哪个学生创建此付款。我希望能够在创建付款时访问学生的ID,以便在add()方法中操作某些内容。
我的控制器中有一个add()方法。这是add()的当前代码。
public function add() {
if ($this->request->is('post')) {
$this->Payment->create();
if ($this->Payment->save($this->request->data)) {
$this->Session->setFlash(__('The payment has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The payment could not be saved. Please, try again.'));
}
}
$students = $this->Payment->Student->find('list');
$this->set(compact('students'));
}
付款表格代码
<?php echo $this->Form->create('Payment'); ?>
<fieldset>
<legend><?php echo __('Add Payment'); ?></legend>
<?php
echo $this->Form->input('student_id');
echo $this->Form->input('date');
echo $this->Form->input('total', array('default' => '0.0'));
echo $this->Form->input('notes');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
答案 0 :(得分:2)
您应该能够以
的身份访问该ID$this->request->data['Payment']['student_id']
这样的事情:
public function add() {
if ($this->request->is('post')) {
$this->Payment->create();
$student_id = $this->request->data['Payment']['student_id'];
// Do something with student ID here...
if ($this->Payment->save($this->request->data)) {
$this->Session->setFlash(__('The payment has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The payment could not be saved. Please, try again.'));
}
}
$students = $this->Payment->Student->find('list');
$this->set(compact('students'));
}
答案 1 :(得分:1)
我发现对于导航CakePHP的大型多维数组非常有用的一个策略是在开发中经常使用debug()
函数。
例如,在add()方法中,我会做类似的事情:
if ($this->request->is('post')) {
debug($this->request->data);
die;
}
然后,您将能够在add()方法完成之前查看Student id隐藏的位置并使用它。我不知道您的数组的确切结构,但很可能您应该能够执行以下操作:
$student_id = $this->request->data['Payment']['Student']['id'];
首先检查debug()的输出(在提交表单之后),以确定数组中所需数据的位置。