Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - Model is not saving in foreach loop in Yii2

Tags:

php

yii2

I have a variable

I have run foreach loop for every item

$tags = ['#first_Tag','#second_tag','#third_tag','#fourth_Tag'];
foreach ($tags as $t) :

  $model = new Tags;

  $model->tag_name = $t;

  $model->save(); //yii2

endforeach;

this function only saves the last item which is #fourth_Tag. Can any one have solution about this. Thanks in advance.

like image 756
Ilyas karim Avatar asked Aug 23 '15 11:08

Ilyas karim


1 Answers

Try this..

$tags = ['#first_Tag','#second_tag','#third_tag','#fourth_Tag'];
$model = new Tags;

foreach ($tags as $t) :

  $model->id = NULL; //primary key(auto increment id) id
  $model->isNewRecord = true;
  $model->tag_name = $t;

  $model->save(); //yii2

endforeach;
like image 159
GAMITG Avatar answered Sep 17 '22 03:09

GAMITG