Update multiple model through a single form

前端 未结 1 626
野的像风
野的像风 2021-01-24 20:29

Hey guys please help me in this I want to Update two tables data through a single form but the data is updating only in a single table and inserting in second t

相关标签:
1条回答
  • 2021-01-24 20:35

    Controller Code Explanation.

    <?php
        $data = $this->Question->findById($id);
    

    above will return all the question and associated answer array as below.

    Array
    (
        [Question] => Array
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
        [Option] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [quesion_id] => 121
                    [body] => The Kungwooness is not so Gwooish
                    [created] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [quesion_id] => 121
                    [title] => More on Gwoo
                    [created] => 2006-05-01 10:41:01
                )
        )
    )
    

    Now, all we need to do is build our form (let's make something really simple):

    echo $form->create('Question', array('action' => 'edit'));
    foreach($this->data['Option'] as $key => $value)
    {
        echo $form->input('Option.'.$key.'.name');
        echo $form->input('Option.'.$key.'.id');
    }
    echo $form->end('Save All');
    

    It is built from our $this->data array and follows exactly the right format, which allows the saveAll() method to work correctly.

    Now post it and see i am sure it will work now.

    Cheers.

    0 讨论(0)
提交回复
热议问题