I, have successfully implemented uploadify script into my app, files are being uploaded to /web/uploads folder.
I have entity Order witch can have multiple attachments OneToMany-unidirectional relation to File entity, uploadify is a part of Order form.
Now, I would like to transform ulpoaded files into File entity presisted to DB, and set it as related to Order that was created with form.
I suppose I have to add some kind of form ID to Order entity and persist it to DB, so uploadify can send this ID and I'll know witch files are related to witch form instance (Maybe use of CSRF token?)
In general, I have no clear idea of how to implement this feature, my english isn't very good, hope everyone will understand my intentions, I'll be thankful of any help or hints on implementation.
I have done this like that:
Each Order entity on construct gives an unique ID base64_encode(microtime()) witch is persisted into DB, then on files form I get this ID from DB and pass it with uploadify postData, them I have clear link witch file was uploaded to witch Order
Related
Working on an application in laravel that has the capability to create article and add photos. With photos you can add a main one to the article or just add photos to a repository.
Once an article is saved to the database it redirects to a view that will allow you to associate a photo with the recently saved article.
I'm wanting to send an some kind of identifier from the create article view to the photo controller method so it will know what to respond with. Then from the photo controller method send that same identifier to the add photo view so when the photo is submitted to the database the controller knows it's a primary article photo.
I'm trying to avoid having to write several different methods that contain a lot of the same code. I would rather the photo controller be able to tell that the photo is a primary article photo and it knows what to do with it.
Any help would be greatly appreciated.
I recommend you to have hidden form field in the view that contains the identifier and photo controller will store it in the session that any other controller can read later.
I want to create universal multiple file uploader, but I don't know what approach should I choose to develop it.
I have Attachment model and attachments table in the database. Also I have model Item and table items. These two a connected via item_attachment table (maybe it will be morph connection in the future, so many models can have attachments).
I thought about several approaches (examples are for create form for item):
Use some js uploader (like Dropzone) and develop separate controller
to handle uploads (AttachmentsController). So when I upload files,
controller will return json response with id's to js, and then they
will be inserted to the form as hidden elements. After submitting
form, they will be attached to the item.
Don't use js, and process uploads directly in ItemsController.
Don't use js, develop a Service, where uploads will be handled and
call it's methods in ItemsController.
What can you recommend? Maybe you have your approach?
Thanks
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:
Enter some post specific informations (like title, content..)
upload with jQuery/AJAX many images for the post.
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.
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.
Not really sure where to start on this one and a day of google searches hasn't helped much.
I am working on a submission form where my logged in users can submit data in form fields and attach a number of files. These files should be available to the users across many submissions so as not to clog up my database with duplicate files and for user convenience not having to reupload files many times. The files will be mostly PDFs and word docs.
My question is, what is the most elegant solution to letting my user select a previously submitted file for their current submission and to "attach" or associate that file with multiple submissions? Some drop down box that shows previous uploads seems best and if you agree any code snippets would be super helpful.
I've decided to store the document files as blobs in their own table. I'm running php and MySQL on Linux with godaddy.
Thanks for advice on where to look for code samples in advance. Just haven't been able to find what I'm looking for. Is JavaScript necessary here?
A better way to handle file uploads is to simply store the file-name (which should be generated as unique) in the database and store the actual file in a directory. That way, you won't have this issue of storing it in a BLOB. Is there any reason you are doing that over this method?
Now, if you output a SELECT form element with a list of filenames stored in the database, a user can simply select one and all you need to do is store that filename in the new row. Then, all rows that point to that file name will point to the same file, with no need to upload it.
If you want to allow multiple file uploads or "attachments" in this case, then you are going to need to create a one-to-many relationship in the database. Each submission row will have many rows in another table that point to it, all referencing the corresponding files uploaded. Such that any submission can have zero, 1, 2, or more files attached. If they select files that already exist, great, just copy the username as stated above. IF its a new file, then you should upload that file and add its name to the database.
Javascript isn't required but it would be nice - to add interactivity to your form. If you want to allow for a user to click say, "Add New File", and have a new file upload form element appear.
Start with a a form. Add a select with the list of files associated with that user. Add another form element that allows for file upload. Have all of your other stuff as normal.
On page view, populate the select with the filenames and put the row IDs for that file in the <option value="file_id_xxx">....
In your server script, detect if a file was uploaded; if so, complete the process of storing the file and set the file reference as the associated file for the submission; if not, check that a file was selected from the dropdown and if so, save the reference from the select with previous uploaded files with the submission instead.
Javascript may help in disabling the select or file upload input, depending. But it's not necessary.
I have an Entity on Symfony that has also a file "attached" to it.
I want to save the file with the id of the created Entity.
The problem is, that I will know the Entity's ID only after doing "dosave()" on that
Entity form.
Is there any way to save the file after doing the "dosave()" but still write the code
as an override for the Entity's form code?
Assuming you are using symfony's bundled file widget, then you can't do this, because of the way the form save process works. In short, the file is saved by the validator, but by the nature of it being a validator, the data hasn't been saved yet!
I've worked around this before by using a temporary filename for the file if the object is new, and then after save has completed (but still within an overload in the form class), move it to the real location now that you know the id.