Whether correctly I understand the essence of downloading images to products?

Hello, uti)

Learn yii2, faced with the task of uploading photos for products.

There are 2 tables(exaggerated):
`product` - id | name (ID and name of commodity)
`product_photo` - id | product_id | image of the entry(ID, item ID, link to image)

The question is how to add the uploaded photos to the product? Idea so far:
In the model of the product Product.php add a property $images. In the derivation of the model of the goods (or edit in the admin) - to display an additional input for multiple download:
<?= $form--->field($model, 'images[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

When sending, the model of the product, use to connect the ProductPhoto model. And while processing these files, add a record in DB using this model.

That's right, I think? Or is there a more elegant solution?
June 26th 19 at 14:13
2 answers
June 26th 19 at 14:15
Good morning.
In principle, correct.
First do as You described.
Earn - looking for a more elegant solution.
But you can share it.
First create the entry in the base product, then, once You are thrown on the page to view the created item, attach the image. Just on the view page of product add form to upload images.
The options can be many, choose the one which You like best.
Thank you, will do so) - zachariah34 commented on June 26th 19 at 14:18
June 26th 19 at 14:17
On the topic API

You will have the methods POST (create item), GET (get), PUT (update data item).
And that's when the PUT you will not be able to send the file, as in the canons and http, the files only go through the POST.
To work on the API made a separate method to load the image.

Let's say the path for working with the product: /api/v1/goods (and the corresponding post, get..)
To download the image, you'll need something like PUT: /api/v1/goods/upload...

What would it worked, first create the item in essence goods, then the image is loaded and written to goods_photo.
ID of goods_photo write in the corresponding field of the entity goods (goods_photo_id)

You can of course crutches ponadelat and write everything via POST.
understood, thank you for your time) - zachariah34 commented on June 26th 19 at 14:20

Find more questions by tags YiiMySQLPHPData structures