M
M
MarkBogrov2017-03-22 22:02:47
Yii
MarkBogrov, 2017-03-22 22:02:47

How to attach a multiple image to the material on the site?

Tell me how best to organize the attachment of multiple images to an object?
There is an article object to which any number of images is attached via file imput / multiple.
I upload it to the server like this:

public function upload()
    {
        if ($this->validate()) {
            foreach ($this->imageFiles as $file) {
                $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
                $files[] = $file->baseName . '.' . $file->extension; // Пока что, просто для получения массива имён
            }
            return $files;
        } else {
            return false;
        }
    }

Actually a question: how to carry out communication with images and my object?
When the image is one, it's clear - I filled in the image name in the same table and that's it. But what to do when there are many images, and they can be edited during the life of the object - added and removed one by one or several? Is it worth starting a separate class for images and treating each uploaded as an object with its own row with a database table or converting an array of names into a string and storing it in the same one cell in a common table, but then how to update one at a time? How is it humanly done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-03-22
@MarkBogrov

Good evening.
Create a separate table, link by material id.
Also create a separate controller and model for images, a separate crud.
Although it's how you like it.
Images that belong to a particular material can be displayed using the gridview on the material view page (in the admin).
PS Or use a visual editor like ckeditor for this purpose. Images are placed directly in the content of the material, it is convenient to edit, do not bother with downloading.

A
Alexey, 2017-03-22
@masterfreelance

it's better to make a separate table for image names and paths and link it one-to-many with the articles table. And, accordingly, its own model for working with an image table. My IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question