学生HasMany支付和支付BelongsTo学生。在创建付款时,我必须指出我正在为哪个学生创建此支付。在创建付款时,我希望能够访问学生的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')); ?>
发布于 2014-02-06 04:43:42
您应该能够访问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'));
}
发布于 2014-02-06 04:40:11
对于导航CakePHP的大型多维数组,我发现一个非常有用的策略是使用经常正在开发的debug()
函数。
例如,在add()方法中,我将执行如下操作:
if ($this->request->is('post')) {
debug($this->request->data);
die;
}
然后,您将能够看到该学生id隐藏的位置,并在add()方法完成之前使用它。我不知道数组的确切结构,但最有可能的情况是,您应该能够这样做:
$student_id = $this->request->data['Payment']['Student']['id'];
只需首先检查debug()的输出(在提交表单之后),以确定您想要的数据在数组中的位置。
https://stackoverflow.com/questions/21593859
复制相似问题