Cakephp在一个模型中保存多个记录

时间:2013-08-02 00:09:08

标签: cakephp cakephp-2.0

我在单个表中保存多个记录时遇到问题。这是我的表格:

<?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

我不确定这里出了什么问题。我想因为这个警告,我无法保存所有数据。

1 个答案:

答案 0 :(得分:4)

您是否尝试使用saveMany而不是saveAll?