how to insert multiple rows using cakephp
Posted
by Paul
on Stack Overflow
See other posts from Stack Overflow
or by Paul
Published on 2010-02-19T23:13:53Z
Indexed on
2010/04/10
5:03 UTC
Read the original article
Hit count: 409
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
© Stack Overflow or respective owner