Adib Aroui
Adib Aroui

Reputation: 5067

ManyToOne relationship where the child entity serves for AJAX file uploading

I have two entities: Image and Post, that are linked by a ManytoOne relationship. The entity Image is used to handle file upload and store upload relative data (like absolute path...). I am using Symfony2 Cookbook tutorial dealing with file uploads.

I am now building a form that allows a user to:

  1. Enter some post specific informations (like title, content..)

  2. upload with jQuery/AJAX many images for the post.

  3. Send the the whole form by button click.

I am still not finding the right approach to implement this solution regarding data persistence in the database.The problem for me is:

In the entity Image, an attribute ($post_id) is used as Foreign Key and will store the post id. The user will upload many images before the entity post is persisted. All the instances of Image created each time will not contain a value for post_id. In my opinion, performance will be affected if I:

Any suggestions are highly appreciated.

Upvotes: 0

Views: 236

Answers (1)

ggavrilut
ggavrilut

Reputation: 378

Why don't you use this way of working with your forms?

http://symfony.com/doc/current/cookbook/form/form_collections.html

And in this way you only upload the images with the entire form.

If you need to preview the photos as the user selects them in the file inputs than you can check this answer

How to preview image before uploading in jquery

But it will not work on older browsers like IE8, IE9.

Upvotes: 1

Related Questions