Reputation: 207
I have problem to save multiple records in a single table. This is my form:
<?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>
and this is my controller:
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.');
}
}
This is my data structure:
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'
)
)
)
I got this warning:
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
I'm not sure what goes wrong here. I guess because of this warning, I cannot save all the data.
Upvotes: 1
Views: 7591