Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I handle uploads centrally and sanely in my web app?
    text
    copied!<p>I am creating a web application, which is basically a very specialised CMS. It's written in PHP with Codeigniter (though the specifics are not important) and is structure according to the MVC pattern.</p> <p>Many entities in this webapp require the ability to have uploads, e.g pages, articles etc.</p> <p>I have an upload controller which works fine on its own. How it functions is that we have a media table which records meta data about an upload, and then a number of other tables which map a specific media item to an article/page etc so <code>articles_media</code>, <code>pages_media</code> etc. These tables just contain a <code>media_id</code> and an <code>article/page/etc_id</code> </p> <p>I clearly don't want to copy and paste the upload code throughout the app so when you are on an article_create view, for example, there is a link to the upload controller which opens in a pop up allowing you to upload an image. </p> <p>The problem with this is that at the point at which you want to upload an image you haven't actually created the article/page etc. so there is no ID that can be passed to the upload controller to map the uploaded media to the specific article/page etc.</p> <p>My "solution" which feels completely insane, is to add an <code>is_published</code> field to the models that require uploads and when you get to the create page to instantly create it with all blank values except <code>is_published = 0</code> and then I have an ID I can pass to the uploader.</p> <p>This does work but it feels sub-optimal, for starters I'm going to [potentially] end up with many blank rows in the database.</p> <p>How do sensible people normally do handle this? I don't require any specific implementation help, I'll figure out the required PHP myself but my brain cannot seem to come up with a logical solution to the overall problem.</p> <p>I thought my mapping between media, articles_media and articles was neat but maybe this is also part of the problem...?</p> <p>In theory I'd like a user to be able to upload an arbitrary number of media files, though in practice this is actually meant to be quite limited, as in you can only have one upload with one article etc.</p> <p>I would appreciate informed opinions - I'm assuming there is a [relatively] straightforward "best practice" to this problem that I just can't seem to come up with myself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload