我有問題在一個表中保存多個記錄。這是我的表格:Cakephp在一個模型中保存多個記錄
<?php echo $this->Form->create('Attendance');?>
<table>
<tr>
<th>Student</th>
<th>Attendance</th>
</tr>
<?php foreach ($students as $key => $student): ?>
<tr>
<td>
<?php echo $student['LectureParticipant']['name']; ?>
<?php echo $this->Form->input('Attendance.'.$key.'.student_id', array('type'=>'hidden', 'value'=>$student['LectureParticipant']['student_id'])); ?>
<?php echo $this->Form->input('Attendance.'.$key.'.lecture_id', array('type'=>'hidden', 'value'=>$student['LectureParticipant']['lecture_id'])); ?>
<?php echo $this->Form->input('Attendance.'.$key.'.date', array('type'=>'hidden', 'value'=>date("Y-m-d"))); ?>
</td>
<td>
<?php
echo $this->Form->input('Attendance.'.$key.'.participant', array(
'label' => false,
'div' => false,
'type' => 'select',
'options' => array(
'1' => 'Present',
'0' => 'Absent',
),
));
?>
</td>
</tr>
<?php endforeach; ?>
</table>
<center>
<?php echo $this->Form->submit('Save', array('class'=>'greenBtn', 'div'=>false));?>
<?php echo $this->Html->link("Cancel", array('action' => 'viewTeacherClass'), array('class' => 'button redBtn')); ?>
<?php echo $this->Form->end();?>
</center>
,這是我的控制器:
if ($this->request->is('post')) {
if ($this->Attendance->saveAll($this->request->data)) {
$this->Session->setFlash('Attendance has been saved.');
$this->redirect(array('controller'=>'programmes', 'action' => 'viewTeacherClass'));
} else {
$this->Session->setFlash('Unable to add attendance.');
}
}
這是我的數據結構:
array(
'Attendance' => array(
(int) 0 => array(
'student_id' => '1',
'lecture_id' => '2',
'date' => '2013-08-02',
'participant' => '1'
),
(int) 1 => array(
'student_id' => '2',
'lecture_id' => '2',
'date' => '2013-08-02',
'participant' => '1'
)
)
)
我得到了這樣的警告:
Warning (2): array_keys() expects parameter 1 to be array, null given [CORE\Cake\Model\Model.php, line 2045]
Warning (4096): Argument 1 passed to Hash::numeric() must be an array, null given, called in C:\wamp\www\ibae\lib\Cake\Model\Model.php on line 2045 and defined [CORE\Cake\Utility\Hash.php, l
我不確定這裏出了什麼問題。我猜是因爲這個警告,我無法保存所有的數據。
看看2045行['Model.php']中的代碼(https://github.com/cakephp/cakephp/blob/a643295e4c48681d26d775d6f50a395e194eaf04/lib/Cake/Model/Model.php#L2045),' $ data'參數直接傳遞給'array_keys()',所以'$ this-> request-> data'似乎是'null'。 – ndm
我已經調試過($ this-> request-> data)並且它不爲空 – Najmi
好吧,如果確實如此,並且您發佈的代碼實際上是觸發錯誤的代碼,那麼請檢查在你的特定的CakePHP版本中你應該總是提到確切的版本號,在Model Model的saveAll()之前做一些調試(例如'var_dump($ data)') '在控制器中,在'Model :: saveAll()'方法內部檢查方法實際接收的方法),如果這不能讓你在軌道上解決問題,那麼請用你的調試更新你的答案結果。 – ndm