S
S
Stepan2015-02-16 16:17:44
MySQL
Stepan, 2015-02-16 16:17:44

(Yii2) Only the last instance is written to the database, but all are saved?

if ($photo->load(Yii::$app->request->post())) {

            $photo->photo = UploadedFile::getInstances($photo, 'photo');

            foreach($photo->photo as $ph){
                $name =  md5( $ph->baseName).'.'.$ph->extension;
                $ph->saveAs('uploads/' . $name );
                $ph->name = $name;
                $photo->photo = $ph;
                $photo->save();
            }

            return $this->redirect(['photo', 'id' => $model->id]);

I understand that only the last one records, but I don’t understand HOW to record everything. Help whoever can

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Bay, 2015-02-16
@kawabanga

First, look at what you get in $_POST files

A
Alexander Makarov, 2015-02-17
@SamDark

Don't get hung up on the AR model in this case. Create a separate form model.

O
Oleg Titarenko, 2015-02-17
@olegtytarenko2

Because the request to the server should happen like this.
The output should look like

$_POST[POST_NAME][0][NAME];
$_POST[POST_NAME][1][NAME];
$_POST[POST_NAME][2][NAME];

//Далее расфасовуешь
foreach($_POST[POST_NAME]  as $query) {
if($model->load($query)) {
$model->save();
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question