Monday, April 10, 2017

How to store images for not existing entities in database?

Let's say I have table entities with id and title.

And I want to attach images to these entities using this table

images:
    id - primary key of image
    filename - path to image on local filesystem
entity_images
    id_entity - foreign key to `entities.id`
    id_image - foreign key to `images.id`

The problem is: images must be added during creation of entities (user sees form, where he types title and selectes images)

Solution, which I use: upload images via ajax and then during creation pass only ids of these images (there can be a lot of them, so doing this through single POST request is not the best idea)

Problem 1: what if user does not create entity after uploading image?

Problem 2: during image upload I create different versions of image(big, medium and small). And when it's done, I create record in database. But what if something goes wrong? Record is written to database, but filesystem cache is not written yet. There is record in database, but no file on disk. Or file is written but database record can not be created due some reason. What are solution for this problem? May be there is something like transaction for this?



via changer

Advertisement