CakePHP 1.3.20: saveAll() for Multi data in form is not work

719 views Asked by At

I use CakePHP 1.3.20. I try to make the add function like:

I have 4 fields "A,B,C,D" to insert. When I want to add some more data I use JQuery to add more fields into the view. So in that time I will have "A0,B0,C0,D0" "A1,B1,C1,D1"...and more.

echo $this->Form->input('Model.1.A',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.B',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.C',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.1.D',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));

echo $this->Form->input('Model.2.A',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.B',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.C',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));
echo $this->Form->input('Model.2.D',array('label'=>'','autocomplete'=>'off','id'=>'advoidance_word','class'=>'form-control top noradius'));

In controller I made:

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    if ($this->Model->saveAll($this->data)) {
                        debug($this->data);
                        $this->Session->setFlash('Your word has been saved.');
                        $this->redirect(array('action' => 'index'));
                    }
                }
            }
        }

My dubug($this->data)

Array
(
    [Model] => Array
        (
            [0] => Array
                (
                    [A] => A
                    [B] => B
                    [C] => C
                    [D] => D
                )

            [1] => Array
                (
                    [A] => A1
                    [B] => B1
                    [C] => C1
                    [D] => D1
                )

        )

)

and I use MSSQL Server 2012 as database. It has no errors. Just any data is inserted to database. What is false in this case? Please help me !

1

There are 1 answers

0
TommyDo On

Totally I found it. I have already made it right but I failed in little thing.

All my code in view is corrected. We will use:

echo $this->Form->input('**Model**.**1**.**Fieldname**',array(...));

For the insert form.

But in the controller we have to change a little bit.

From (As above)

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    if ($this->Model->saveAll($this->data)) {
                        debug($this->data);
                        $this->Session->setFlash('Your word has been saved.');
                        $this->redirect(array('action' => 'index'));
                    }
                }
            }
        }

To this one:

function add() {
            if (!empty($this->data)) {
                if(isset($_POST['create'])){
                    foreach($this->data as $data) {
                        if ($this->MAvoidanceWord->saveAll($data)) {
                            debug($this->data);
                            //$this->Session->setFlash('Your word has been saved.');
                            //$this->redirect(array('action' => 'index'));
                        }
                    }   
                }
            }
        }

The different between is: in old code, I didn't have foreach() to query each data in the array so Cake don't know how to insert. So I made it and it was successful. Hope my topic is help for some one. CakePHP 1.3 didn't have much experience documents. Thank everybody to see.