Hello,
I need to ensure the images uploaded, then insert into DB (id, car, name), the issue in the loop is inserting only the first item, then it updates the same record only.
<?php
foreach ($model->car_image as $image) {
$transaction = Yii::$app->db->beginTransaction();
Yii::info("this for image name:" . $image->getBaseName(), "app");
if (in_array($image->extension, ['jpg', 'jpeg'])) {
if ($image->size <= $maxFileSize) {
$model->name = "t_" . time() . "_i_" . uniqid() . '.' . $image->extension;
if ($image->saveAs($imageDir . '/' . $model->name)) {
$saveIt = true;
} else {
throw new InvalidValueException('Failed to create the object for unknown reason. [APIx000]');
}
} else {
throw new InvalidValueException("Failed to save! Maximum file size is $maxFileSize. [APIx003]");
}
} else {
throw new InvalidValueException("Failed to save! Only jpg file format allowed. [APIx004]");
}
if ($model->save(false) and $saveIt) {
$transaction->commit();
$imageId[] = $model->id;
} else {
$transaction->rollBack();
throw new InvalidValueException("Failed to save the data due to validation. [APIx002]");
}
}
Any idea how to solve it?!