开发者

edit multiple models cake php

trying to edit multiple models

The Controller

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('acti开发者_运维技巧on' => 'index'));
        } else {
            $this->Session->setFlash(__('The qnote could not be saved. Please, try again.', true));
        }
    }

The Form

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

I am trying to edit and update information in associated models , Qnotes and Step The information show up in the form. however when i submit the form. the

the Qnote information is saving with out any problem . however the step information is not updating

The models are associated. with Steps belong to Qnote, QNote has Many Steps


Your form include '0' for all the Step inputs.

    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');

And in your controller action, you need to call saveAll() instead.

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


Try this to load different model. :) var $uses = array('Qnote', 'Step', 'modelName');


If you want to save data in mutiple model you have to call the model in controller. Using

$this->loadModel('Step');

then do the save part like below. You have called save function for an object twice.

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));
    }
}


If the models are associated, you can save the whole thing at once (in all the models concerned) just by using the saveAll() function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜