在控制器方法CAKEPHP中访问变量
学生有许多付款和属于学生的付款。创建付款时,我必须指出我为哪个学生创建此付款。我希望在创建付款时能够访问学生的id,以便在add()方法中进行操作 我的控制器中有一个add()方法。以下是add()的当前代码 付款表格代码在控制器方法CAKEPHP中访问变量,cakephp,Cakephp,学生有许多付款和属于学生的付款。创建付款时,我必须指出我为哪个学生创建此付款。我希望在创建付款时能够访问学生的id,以便在add()方法中进行操作 我的控制器中有一个add()方法。以下是add()的当前代码 付款表格代码 <?php echo $this->Form->create('Payment'); ?> <fieldset> <legend><?php echo __('Add Payment'); ?></le
<?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')); ?>
我发现在CakePHP的大型多维数组中导航非常有用的一种策略是在开发中经常使用
debug()
函数
例如,在add()方法中,我将执行以下操作:
if ($this->request->is('post')) {
debug($this->request->data);
die;
}
$student_id = $this->request->data['Payment']['Student']['id'];
然后,您将能够看到该学生id隐藏的位置,并在add()方法完成之前根据需要使用它。我不知道阵列的确切结构,但很可能您应该能够执行以下操作:
if ($this->request->is('post')) {
debug($this->request->data);
die;
}
$student_id = $this->request->data['Payment']['Student']['id'];
只需先检查debug()的输出(提交表单后),以确定所需数据在数组中的位置。您应该可以通过以下方式访问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'));
}
能否在视图中添加付款表单的代码?如果您选择的是该表单中的学生,那么它应该包含在
$this->request->data
谢谢!我以后一定会用这个。现在开始工作了