I want to save 10 times in Cakephp using Save function with for loop but it doesn't seem to work. It only saves one data.
How can we make it so it saves multiple data at one call?
Below is my code...
for($i=0;$i<10;$i++){
$unique_id = $this->_unique($userData['User']['id']); // Unique id with userid as initial prefix
$this->data['Invite'] = array('invited_user' => $userData['User']['id'], 'unique_id' => $unique_id);
$this->User->Invite->save($this->data['Invite']);
}
You need to call Model::create( ) for each iteration of the loop. IE:
<?php
for( $i=0; $i<10; $i++ ){
$this->User->Invite->create( );
$unique_id = $this->_unique($userData['User']['id']);
$this->data['Invite'] = array('invited_user' => $userData['User']['id'], 'unique_id' => $unique_id);
$this->User->Invite->save($this->data['Invite']);
unset( $this->data[ 'Invite' ] );
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With