0

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:

  • update all the rows in image table after persisting an instance of Post.

  • Create an empty Post instance first, use its id in the Image instances, then update Post instance.

Any suggestions are highly appreciated.

Adib Aroui
  • 4,981
  • 5
  • 42
  • 94
  • While I agree, 'technically' performance will be affected by those solutions, unless the users are loading thousands of pictures, it will probably be negligible. – Lighthart Jan 23 '14 at 20:54
  • @Lighthart, I agree with you. Additionnaly, the second approach will execute less sql queries. But I a think there is likely another way more appropriate to my situation. thanks and regards – Adib Aroui Jan 23 '14 at 22:23
  • 1
    I tried to answer a similar question in http://stackoverflow.com/questions/18143432/implementation-of-fully-functional-media-uploading-in-web-application/18149369#18149369 – devsheeep Jan 24 '14 at 11:10

1 Answers1

1

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.

Community
  • 1
  • 1
ggavrilut
  • 378
  • 1
  • 9
  • Well, after reading the content of links, I dont find a solution to my problem. In fact, I think AJAX is forcing me to find another approach. What do you think? thanks – Adib Aroui Jan 27 '14 at 23:49
  • What exactly doesn't suit what you want to do? Isn't it possible for you to upload all the images together with the entire form (because thos why form_collections in Symfony2 allows you to do, add, remove photos inside parent form) – ggavrilut Jan 28 '14 at 07:18