2012-10-22 72 views
1

需要您的幫助,通過在cakephp 2.0中saveAll解決我的問題。我有3個型號:數據表,數據表和數據表值。我需要從數據表中刪除最後插入的ID。我只能從Datasheetsheading讀...cakephp 2.0 saveAll with getlastid()

我的控制器:

public function add_datasheet($id = null) { 
    $this->set('datasheetsheadings', $this->datasheetsheading->find('all', array('limit' => 0 .','. 9))); 
    $this->set('datasheetsheadings2', $this->datasheetsheading->find('all', array('limit' => 9 .','. 9))); 
    $this->set('id', $id); 
    if ($this->request->is('post')) { 
     if ($this->datasheet->save($this->request->data)) { 
      $this->datasheetsvalue->saveAll($this->request->data['datasheetsvalue']); 
      $this->Session->setFlash('Your Datasheet >> Lot: ' . $this->request->data['datasheetsvalue'][1]['value'] . ' << has been saved.'); 
      $this->redirect(array('action' => 'Datasheets/' . $this->request->data['datasheet']['id_tbl_articles'])); 
     } else { 
      $this->Session->setFlash('Unable to add your datasheet.'); 
     } 
    } 
} 

我的觀點:

<?php echo $this->Form->create('datasheet', array('action' => 'add_datasheet')); ?> 
    <fieldset> 
     <legend><?php echo __('Neues Datenblatt'); ?></legend> 
     <?php 
     $value_id = 0; 
     echo $this->Form->input('datasheet.comment', array('label' => 'Kommentar')) . '<hr>'; 
     echo '<div style="width:300px;float:left;">'; 
     foreach ($datasheetsheadings as $datasheetsheading) : 
      $value_id = $value_id +1; 
      echo $this->Form->input('datasheet.id_tbl_articles', array('type' => 'hidden', 'value' => $id)); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_articles', array('type' => 'hidden', 'value' => $id)); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheets', array('type' => 'hidden')); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheetsheadings', array('type' => 'hidden', 'value' => $datasheetsheading['datasheetsheading']['id'])); 
      echo __('<div style="width:140px;color:#FFF;float:left;">' . $datasheetsheading['datasheetsheading']['heading'] . '</div><div style="width:160px;color:#FFF;float:left;text-align:right;">'); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.print', array('type' => 'checkbox', 'label' => 'Im Datenblatt anzeigen', 'style' => 'color:#FFF;')); 
      echo '</div><div style="clear:left;"></div><div style="margin-top:-30px;">'; 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.value', array('label' => '')); 
      echo '</div>'; 
     endforeach; 
     echo '</div>'; 
     echo '<div style="width:170px;float:left;"></div>'; 
     echo '<div style="width:300px;float:left;">'; 
     foreach ($datasheetsheadings2 as $datasheetsheading2) : 
      $value_id = $value_id +1; 
      echo $this->Form->input('datasheet.id_tbl_articles', array('type' => 'hidden', 'value' => $id)); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_articles', array('type' => 'hidden', 'value' => $id)); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheets', array('type' => 'hidden')); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.id_tbl_datasheetsheadings', array('type' => 'hidden', 'value' => $datasheetsheading2['datasheetsheading']['id'])); 
      echo __('<div style="width:140px;color:#FFF;float:left;">' . $datasheetsheading2['datasheetsheading']['heading'] . '</div><div style="width:160px;color:#FFF;float:left;text-align:right;">'); 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.print', array('type' => 'checkbox', 'label' => 'Im Datenblatt anzeigen', 'style' => 'color:#FFF;')); 
      echo '</div><div style="clear:left;"></div><div style="margin-top:-30px;">'; 
      echo $this->Form->input('datasheetsvalue.' . $value_id . '.value', array('label' => '')); 
      echo '</div>'; 
     endforeach; 
     echo '</div>'; 
     echo '<div style="clear:left;"></div>'; 
     echo $this->Form->end(__('Sichern')); 
    ?> 
    </fieldset> 

通過保存在數據表的表,如果存儲了數據表:id(auto_increment)id_tbl_articlescommentcreatedmodified。這完美的作品!
在數據表值表中我得到值:id(auto_increment),id_tbl_datasheetsheadings,id_tbl_articles,valuesprint。只有在id_tbl_datasheets我得到NULL。我用$hasOne,$hasMany,$belongsTo等嘗試了很多。要設置值id_tbl_datasheets也是$this->datasheet->getlastid()。沒什麼作用!當我將saveAll更改爲save時,我得到正確的值,但只有一個。所以我需要功能saveAll
谷歌是你的朋友,發現我不能使用getLastId()saveAll。我能用什麼解決我的問題?

大家都謝謝你的回答!安迪。

回答

0

我得到了答案!

public function add_datasheet($id = null) { 
    $this->set('datasheetsheadings', $this->datasheetsheading->find('all', array('limit' => 0 .','. 9))); 
    $this->set('datasheetsheadings2', $this->datasheetsheading->find('all', array('limit' => 9 .','. 9))); 
    $this->set('id', $id); 
    if ($this->request->is('post')) { 
     if ($this->datasheet->save($this->request->data)) { 
      $last_id = $this->datasheet->id; 
      $val_id = 0; 
      foreach ($this->request->data['datasheetsvalue'] as $data_val_id) { 
       $val_id = $val_id + 1; 
       $this->request->data['datasheetsvalue'][$val_id]['id_tbl_datasheets'] = $last_id; 
      } 
      $this->datasheetsvalue->saveMany($this->request->data['datasheetsvalue']); 
      $this->Session->setFlash('Your Datasheet >> Lot: ' . $this->request->data['datasheetsvalue'][1]['value'] . ' << has been saved.'); 
      $this->redirect(array('action' => 'Datasheets/' . $this->request->data['datasheet']['id_tbl_articles'])); 
     } else { 
      $this->Session->setFlash('Unable to add your datasheet.'); 
     } 
    } 
} 

我現在用saveMany。之前,我做了一個foreach,並用我的var $ val_id設置數據[]。現在它運作完美。
無論如何,謝謝!