M
M
Mikha Pankratov2016-05-21 10:37:40
Yii
Mikha Pankratov, 2016-05-21 10:37:40

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');

My model
$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');

Model
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_/.',
            ],
        ];
    }

view
$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();

and controller
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
        ]);
    }

Who had such a problem and found a solution, please respond) or just who has nothing to do on Saturday)
There is a guess on the riddle) that I am not using the ImageUploadBehavior correctly (but it still saves some magic)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2016-05-21
@user004

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();
                        }
                    }

I'm not familiar with the language and the logic here is not entirely clear
Explain each line, including where something is wrong. If not laziness.
Where is the use of $file ??????

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question