M
M
Mikha Pankratov2015-04-05 02:10:50
Yii
Mikha Pankratov, 2015-04-05 02:10:50

Yii2 how to add many records to a table?

Good afternoon,
I am doing a multi-upload of pictures.
I got an array of pictures... I save them to a folder and try to do a mass addition to the database using the same foreach.

//какие-то штуки :-)
foreach ($model->image as $file) {
//какие-то штуки :-)
                    $file->saveAs($dir . $filename . '.' . $extension);

                    $model->album_id = 1;
                    $model->timestamp = time();
                    $model->save();
                }

But only the last record gets into the table why?
Strangely, according to the idea, foreach twists my array and together with it my $model->save();
Can someone come across this? or post a link to the doc...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-04-05
@frmax

You overwrite because.
This will overwrite the entry:

$db = ModelActiveRecord::findOne();
$db->entry = 123;
$db->save();

This will create a new one:
$db = new ModelActiveRecord()
$db->entry = 123;
$db->save();

M
Mikha Pankratov, 2015-04-05
@frmax

Thank you. Already corrected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question