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.
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 am working on a CMS in Codeigniter and it has the usual fields like title, post content, etc (the page is similar to Wordpress in this way).
I have an image uploader on the page that saves the image via ajax to a MySQL table with basic fields such as image_id, path, size.
There is another table that stores the relationship between an item and an image that requires both an item_id and an image_id.
The problem I have is that the item_id is not known until the page is created/saved. So if a user goes to the Create Post page and uploads an image from there, I can't add the relationship since I don't have an item_id yet.
Some things I am considering:
Every time someone goes to the Create Post page, it automatically creates a blank post in the MySQL table with just the item_id and storing it in a hidden field. The problem I have with this is the database could get filled with a lot of empty posts if a person just clicks Create Post but never fills it out.
Saving the images in the images table, but also save an array of all of their ids for when the Create button is clicked. When that button is click, then add the image relationships.
Hide the image uploader until the post title is filled out, which triggers a page save
Wordpress seems to have solved this but I'm not sure what it does?
Anyone have any better suggestions?
I would say to structure your images table so that it doesn't require a reference to a specific post. Put image-post relationships in a separate table. On your "compose post" screen, have the image uploader return the ID of the newly-inserted image, and when you submit the post, add the new post ID and the image ID to the table for image-post relationships. This also gives you more flexibility to do things like link to one image in multiple posts.
-- edit: upon re-reading, it sounds like this is already how you're storing them. If so, then there shouldn't be a problem. Just add the image-post relationship at the time the post is submitted. If you're worried about orphaned images, see below. --
You're right that this could lead to orphan images, if someone uploads an image and then closes the window without making a post. I wouldn't worry about the images table "filling up," though. MySQL tables can have millions of rows and still perform well.
If you're concerned about collecting unused image files, you could have a periodic maintenance job that removes image files (and the associated table entries) if the upload date is older than, say, 1 week, and the image isn't associated with any posts.
Seems simple enough:
Use 1 form
Collect the data posted
Insert article - record insert id
Insert Image - record image id
Use ids to insert into relationship table
EDIT
As you are using ajax to upload the image, you could use a return value to load the inserted image id into a hidden form field
I need help with a function so that I can upload a picture from my custom CMS, then have it shown together with my post. Look at the picture below:
I want a picture in this image placeholder
I'm not so good at PHP or Mysql so please give me a easy answer.
I'll post my code over here at pastebin, easier for you to watch over there! http://pastebin.com/gBVe9sZV
If you'd like to show a set of images with your post, you'll need to store those images in a table along with the post id that they are associated with.
Upon submitting your form you're probably handling the post data via $_POST. Your images will come across under $_FILES. Once you have performed an insert on your post table, you can call mysql_insert_id() to get the new primary key ID for that post.
The post id can be stored in your images table along with references to your various images. I'm assuming here that you aren't generating your own post id's, however if you are you'd use that instead.
See also Handling File Uploads, on php.net.
Whenever you're querying your database to show the post to your readers, perform a JOIN on the images table based upon a common post id, and you'll have the paths to all related images.
See also Understanding JOINs in MySQL and Other Relational Databases, on sitepoint.com.
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