Reputation: 1559
I am reading a csv file in yii framework. it loops through every word in the file but saves only last word. For example i have following words in my csv file.
First
Second
Third
Fourth
following is my code which loops through the file.
$fileHandler=fopen("upload.csv",'r');
if($fileHandler){
while($line=fgetcsv($fileHandler,1000)){
$model->image_url=$line[0];
$model->save();
}
}
it is only saving value 'Fourth' in my db. Please guide.
Upvotes: 2
Views: 4372
Reputation: 24374
As Chococroc suggested in the comments, You need to initialize your $model
with in the loop rather of out side, since it has saved the value in the first iteration and then updating the same object (i.e. same row in your database) for the rest of the iterations
$fileHandler=fopen("upload.csv",'r');
if($fileHandler){
while($line=fgetcsv($fileHandler,1000)){
$model = new CLASS_NAME;
$model->image_url=$line[0];
$model->save();
}
}
Upvotes: 5