Answer the question
In order to leave comments, you need to log in
Multiloading files and saving to the database. Who saw such an error?
Hello everyone,
I mean multiboot.
Pictures are saved to the server, but there is no record of the file name ... when the name comes) I will
show my data structure
$this->createTable('images', [
'id' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'images_image' => Schema::TYPE_STRING. ' NULL'
]);
$this->createTable('photos', [
'id' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'image_id' => Schema::TYPE_INTEGER. '(10) UNSIGNED NOT NULL',
'photos_image' => Schema::TYPE_STRING. ' NULL',
]);
$this->addForeignKey('fk_photos_image_id','photos','image_id','images','id','CASCADE','CASCADE');
$this->createTable('images', [
'id' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'images_image' => Schema::TYPE_STRING. ' NULL'
]);
$this->createTable('photos', [
'id' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'image_id' => Schema::TYPE_INTEGER. '(10) UNSIGNED NOT NULL',
'photos_image' => Schema::TYPE_STRING. ' NULL',
]);
$this->addForeignKey('fk_photos_image_id','photos','image_id','images','id','CASCADE','CASCADE');
public $photos_image;
public function rules()
{
return [
['photos_image','safe'],
['photos_image', 'file', 'extensions' => 'jpeg, gif, png, jpg', 'maxFiles' => 15],
];
}
public function behaviors() {
return [
[
'class' => '\yiidreamteam\upload\ImageUploadBehavior',
'attribute' => 'photos_image',
'thumbs' => [
'thumb' => ['width' => 180, 'height' => 200],
'large' => ['width' => 600,'height' => 800],
],
'filePath' => '@frontend/private/profile/photo/__original.',
'fileUrl' => '/private/profile/photo/__original.',
'thumbPath' => '@webroot/uploads/profile/avatar/photo_/.',
'thumbUrl' => '/uploads/profile/avatar/photo_/.',
],
];
}
$form = ActiveForm::begin([
'id' => 'Items',
'enableClientValidation' => false,
'errorSummaryCssClass' => 'error-summary alert alert-error',
'options' => ['enctype' => 'multipart/form-data', 'accept' => 'image/*']
]);
echo $form->errorSummary($model);
echo $form->field($model, 'images_image')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*'],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
]]);
echo $form->field($photo, 'photos_image[]')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*', 'multiple' => true,],
])->label(false);
echo Html::submitButton(
'<span class="glyphicon glyphicon-check"></span> ' .
($model->isNewRecord ? 'Create' : 'Save'),
[
'id' => 'save-' . $model->formName(),
'class' => 'btn btn-success'
]
);
ActiveForm::end();
public function actionIndex()
{
$model = new Images();
$photo = new Photos();
if($_POST){
if($model->load(\Yii::$app->request->post()) && $photo->load(\Yii::$app->request->post())){
$model->images_image = UploadedFile::getInstance($model, 'images_image');
if ($model->save()) {
$photo->image_id = $model->id;
$photo->photos_image = UploadedFile::getInstances($photo, 'photos_image');
if ($photo->photos_image && $photo->validate()) {
foreach ($photo->photos_image as $file) {
$p = new Photos();
$p->image_id = $model->id;
//<b>....вот тут происходит мистика и все как бы сохраняется файл, запись в бд, но вот имени файла нет</b>
// $p->photos_image = $photo->photos_image;
$p->save();
}
}
}
}
}
return $this->render('index', [
'model' => $model,
'photo' => $photo
]);
}
Answer the question
In order to leave comments, you need to log in
if ($photo->photos_image && $photo->validate()) {
foreach ($photo->photos_image as $file) {
$p = new Photos();
$p->image_id = $model->id;
//<b>....вот тут происходит мистика и все как бы сохраняется файл, запись в бд, но вот имени файла нет</b>
// $p->photos_image = $photo->photos_image;
$p->save();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question