2013-08-02 57 views
1

我有问题在一个表中保存多个记录。这是我的表格: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

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

+0

看看2045行['Model.php']中的代码(https://github.com/cakephp/cakephp/blob/a643295e4c48681d26d775d6f50a395e194eaf04/lib/Cake/Model/Model.php#L2045),' $ data'参数直接传递给'array_keys()',所以'$ this-> request-> data'似乎是'null'。 – ndm

+0

我已经调试过($ this-> request-> data)并且它不为空 – Najmi

+0

好吧,如果确实如此,并且您发布的代码实际上是触发错误的代码,那么请检查在你的特定的CakePHP版本中你应该总是提到确切的版本号,在Model Model的saveAll()之前做一些调试(例如'var_dump($ data)') '在控制器中,在'Model :: saveAll()'方法内部检查方法实际接收的方法),如果这不能让你在轨道上解决问题,那么请用你的调试更新你的答案结果。 – ndm

回答

4

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

+1

我用这个$ this-> Attendance-> saveMany($ this-> request-> data ['Attendance'])并且它可以工作 – Najmi

+0

伟大的工作@Ayo Akinyemi :)它的作品 – AnNaMaLaI

相关问题