Yii Framework Forum: how to save multiple records in iteration? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to save multiple records in iteration? Rate Topic: -----

#1 User is offline   elliot 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 14-July 11

Posted 02 September 2011 - 03:21 AM

for ( ) {
$this->attributes = $pin;
$this->save();
}

hi, this is not working T______T

it will only save one record... other will update...
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 616
  • Joined: 08-May 09

Posted 02 September 2011 - 04:20 AM

you have to create a new model in the for-loop
1

#3 User is offline   arip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 25-September 11

Posted 11 November 2011 - 03:57 AM

are model must loop?? like
$model[$i]=new Model;

0

#4 User is offline   arip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 25-September 11

Posted 11 November 2011 - 03:59 AM

I have tried it is usefull. :D :D
0

#5 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 07 April 2012 - 05:09 AM

If you don't want to repeat this:
$model = new foobar;
$model->attributes=$_POST['foobar'];
........


Use (assuming your primary key is automatically generated by auto-increment) :
while ($loop!=false) {
$model->setIsNewRecord(true);
$model->setPrimaryKey(NULL);

//then
$model->save(false);
}


It was worked for me
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
1

#6 User is offline   prazi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-June 13

Posted 03 August 2013 - 02:54 AM

View Postsaegeek, on 07 April 2012 - 05:09 AM, said:

If you don't want to repeat this:
$model = new foobar;
$model->attributes=$_POST['foobar'];
........


Use (assuming your primary key is automatically generated by auto-increment) :
while ($loop!=false) {
$model->setIsNewRecord(true);
$model->setPrimaryKey(NULL);

//then
$model->save(false);
}


It was worked for me


Thanks. It was a life saver. Cheers.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users