2011-05-09 53 views
2

試圖編輯多個模型編輯多個模型蛋糕PHP

的控制器

function edit($id = null) { 

    if (!empty($this->data)) { 
    $this->Qnote->save($this->data); 
     if ($this->Qnote->save($this->data)) {   
      $this->data['Step']['qnote_id'] = $this->Qnote->id; 
      $this->Step->save($this->data); 
      $this->Session->setFlash(__('The qnote has been saved', true)); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The qnote could not be saved. Please, try again.', true)); 
     } 
    } 

<?php echo $this->Form->create();?> 
<fieldset> 
    <legend><?php __('Edit Qnote'); ?></legend> 
<?php 
    echo $this->Form->hidden('Qnote.id');  
    echo $this->Form->input('Qnote.subject'); 
    echo $this->Form->input('Qnote.body');  
    echo $this->Form->hidden('Step.0.id'); 
    echo $this->Form->Hidden('Step.qnote_id'); 

    echo $this->Form->Hidden('Step.user_id'); 
    echo $this->Form->input('Step.0.body'); 

?> 

<?php echo $this->Form->end(__('Submit', true));?> 

我想編輯和更新相關車型信息的形式,QNOTES和步驟 信息顯示在表單中。但是當我提交表格。

Qnote信息保存沒有任何問題。但是步驟信息未更新

模型已關聯。與步驟屬於Qnote,QNote有很多步驟

回答

1

您的表單包括'0'的所有步驟輸入。

echo $this->Form->hidden('Qnote.id');  
    echo $this->Form->input('Qnote.subject'); 
    echo $this->Form->input('Qnote.body');  
    echo $this->Form->hidden('Step.0.id'); 
    echo $this->Form->Hidden('Step.0.qnote_id'); 
    echo $this->Form->Hidden('Step.0.user_id'); 
    echo $this->Form->input('Step.0.body'); 

而在您的控制器操作中,您需要調用saveAll()。

if ($this->Qnote->saveAll($this->data)) { 
     ... 
0

如果要將數據保存在多模型中,則必須調用控制器中的模型。使用

$this->loadModel('Step'); 

然後做下面的保存部分。您已經爲對象調用了兩次保存功能。

function edit($id = null) { 

    if (!empty($this->data)) { 
    $save = $this->Qnote->save($this->data); 
     if ($save) {   
     $this->data['Step']['qnote_id'] = $this->Qnote->id; 
     $this->Step->save($this->data); 
     $this->Session->setFlash(__('The qnote has been saved', true)); 
     $this->redirect(array('action' => 'index')); 
    } else { 
     $this->Session->setFlash(__('The qnote could not be saved. Please, try again.', true)); 
    } 
} 
1

試試這個來加載不同的模型。 :) var $ uses = array('Qnote','Step','modelName');

0

如果這些模型是關聯的,則可以通過使用saveAll()函數一次(在所有相關模型中)保存整個事物。