Answer the question
In order to leave comments, you need to log in
(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]);
Answer the question
In order to leave comments, you need to log in
Don't get hung up on the AR model in this case. Create a separate form model.
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 questionAsk a Question
731 491 924 answers to any question