views:

974

answers:

1

In the cakePHP project I'm building, I want to insert a defined number of identical records. These will serve as placeholders records that will have additional data added later. Each record will insert the IDs taken from two belongs_to relationships as well as two other string values.

What I want to do is be able to enter a value for the number of records I want created, which would equate to how many times the data is looped during save.

What I don't know is:

1) how to setup a loop to handle a set number of inserts

2) how to define a form field in cakePHP that only sets the number of records to create.

What I've tried is the following:

function massAdd() {

$inserts_required = 1;
while ($inserts_required <= 10) {
$this->Match->create();
$this->Match->save($this->data);
echo $inserts_required++;
}

$brackets = $this->Match->Bracket->find('list');
$this->set(compact('brackets'));

}

What happens is:

1) at the top of the screen, above the doc type, the string 12345678910 is displayed, this is displayed on screen

2) a total of 11 records are created, and only the last record has the values passed in the form. I don't know why 11 records as opposed to 10 are created, and why only the last records has the entered form data?

As always, your help and direction is appreciated.

-Paul

A: 
  1. in your view try to write soething like

    echo $form->input('Answer.n.title', array('type'=>text'));

in controller write

function add(){
    $this->Answer->saveAll($this->data);
}
  1. in my project I used jQuery, to add a new row without reloading a page.
Aziz
What I've tried is the following:function massAdd() { $inserts_required = 1;while ($inserts_required <= 10) {$this->Match->create();$this->Match->save($this->data);echo $inserts_required++;}$brackets = $this->Match->Bracket->find('list');$this->set(compact('brackets'));}What happens is:1) at the top of the screen, above the doc type, the string 12345678910 is displayed,2) a total of 11 records are created, and only the last record has the values passed in the form.I don't know why 11 records as opposed to 10 are created, and why only the last records has the entered form data?
Paul
I don't know why only the 10th record saved, because due to your code, there must be 10 inserted SAME records...a little tip, before saving just do pr($this->data); and analyze in what format you got your post data.
Aziz