无法将多行保存到关联的模型

时间:2014-10-07 03:09:31

标签: php cakephp

我想在我的关联表中获取值,并且使用我的代码,我在$ this-> request->数据上得到正确的输出。我正确保存到时间表表,但没有记录保存到timesheetLessons表。

时间表表与timesheetLessons有1对多的关系。 看看我认为我只需要指定modelname和savAssociated的文档应该可以工作,但它没有。

我查看了文档和过去的帖子,其中过去的帖子引用了另一种简化避免视图的方法。我确信有一个简单的答案,因为我得到了正确的数据。

http://book.cakephp.org/2.0/en/models/saving-your-data.html

控制器

    class TimeSheetsController extends AppController {

      debug( $this->request->data);
           if ($this->request->is('post') ||$this->request->is('put')) {


              $this->TimeSheet->create();
          if (   !$this->request->data['TimeSheet']['tutor_id'])
          {
              $this->request->data['TimeSheet']['tutor_id']=$tutorId;
          }


            if ($this->TimeSheet->saveAssociated($this->request->data)) {

查看

   <tr>
            <th>id</th>
            <th>tutor id</th>
            <th>date</th>     
         </tr>

      <?php 
              echo $this->Form->create('TimeSheet');
              echo '<tr>';
       // debug($timesheet['TimeSheet']['tutor_id']);
              echo $this->Form->hidden('TimeSheet.tutor_id');
              echo $this->Form->hidden('TimeSheet.id');
              echo   $this->Form->input('TimeSheet.tutor_name', array('label' => 'Tutor name', 'value'=>$tutor_name)); 
              echo   $this->Form->input('TimeSheet.invoice'); 
              echo   $this->Form->input('TimeSheet.note'); 
              echo   $this->Form->input('TimeSheet.abn'); 


          echo '</tr>';
        foreach ($lesson as $key => $item2):
      //  foreach ($lesson as  $item2):   
               echo '<tr>';
               echo   '<td>'.$item2['Lesson']['id'].'</td>'; 
               echo   '<td>'.$item2['Lesson']['tutor_id'].'</td>'; 
               echo   '<td>'.$item2['Lesson']['lesson_date'].'</td>'; 

               echo $this->Form->hidden('TimesheetLessons.'.$key.'.id',
                            array('value'=>$item2['Lesson']['id']));

               echo $this->Form->hidden('TimesheetLessons.'.$key.'.tutor_id',
                            array('value'=>$item2['Lesson']['tutor_id']));

               echo $this->Form->hidden('TimesheetLessons.'.$key.'.lesson_date',
                            array('value'=>$item2['Lesson']['lesson_date']));
          echo '</tr>';

正确输出

array(
    'TimeSheet' => array(
        'tutor_name' => 'teacher2',
        'invoice' => '8',
        'note' => '',
        'abn' => '3242',
        'tutor_id' => '16',
        'id' => '1'
    ),
    'TimesheetLessons' => array(
        (int) 0 => array(
            'id' => '18',
            'tutor_id' => '16',
            'lesson_date' => '2014-10-06'
        ),
        (int) 1 => array(
            'id' => '19',
            'tutor_id' => '16',
            'lesson_date' => '2014-10-13'
        )

2 个答案:

答案 0 :(得分:2)

在保存数据时,请尝试此操作。我希望它可以帮到你。 :)

if($this->request->is(array('post', 'put'))) {
    $update = $this->TimeSheet->updateAll(
        array(
            'TimeSheet.tutor_name' => "'" . $this->request->data['ControllerFunctionName']['tutor_name'] . "'",
            'TimeSheet.invoice' => "'" . $this->request->data['ControllerFunctionName']['invoice'] . "'",
            //add some columns here
            ),
        array('TimeSheet.id' => $this->request->data['ControllerFunctionName']['id'])
    );
    if($update){
        foreach ($this->request->data['TimesheetLessons'] as $x):
            $updateTL = $this->TimesheetLessons->updateAll(
                array(
                    'TimesheetLessons.tutor_id' => "'" . $this->request->data['ControllerFunctionName'][$x]['tutor_id'] . "'",
                    'TimesheetLessons.lesson_date' => "'" . $this->request->data['ControllerFunctionName'][$x]['lesson_date'] . "'",
                    //add some columns here
                    ),
                array('TimesheetLessons.id' => $this->request->data['ControllerFunctionName']['id'])
            );
        endforeach;
        //some setFlash message and redirect
    } else{
        //some setFlash message and redirect
    }
} 

答案 1 :(得分:0)

使用$this->TimeSheet->saveAssociated($this->request->data)。它将达到目的。