Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save multiple times in Cakephp

Tags:

php

cakephp

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']);
            }
like image 317
Passionate Engineer Avatar asked Jul 26 '11 20:07

Passionate Engineer


1 Answers

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' ] );
    }
?>
like image 152
Abba Bryant Avatar answered Nov 07 '22 08:11

Abba Bryant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!