Yii Framework Forum: Saving array of data to database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Saving array of data to database

#1 User is offline   darwin15 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 27-January 15

Posted 27 January 2015 - 04:42 AM

i want to save a textfield value to database, but this textfield is an array of data...... how to save this data to database...


note:this should be saved per row. can anyone help me??? plssss
0

#2 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,059
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 27 January 2015 - 12:26 PM

If your input text are (for example 999)

<input type="text" name="DataModel[0][textfield]" />
<input type="text" name="DataModel[1][textfield]" />
<input type="text" name="DataModel[2][textfield]" />
<input type="text" name="DataModel[3][textfield]" />
...
...
<input type="text" name="DataModel[999][textfield]" />


in your controller:

if(isset($_REQUEST['DataModel']))
{
     foreach($_REQUEST['DataModel'] as $dataModel)
     {
          $m = new DataModel();
          $m->textfield = $dataModel['textfield'];
          $m->save();
     }
}

Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#3 User is offline   Yaasky 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 09-March 15
  • Location:Abuja, NG

Posted 10 March 2015 - 05:02 AM

Create models with each of the "DataModel" entries from the form and save the newly created model.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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