Linking an entity to have several images and videos - php

I'll try to explain my problem as much as I can.
Let's say I have an entity called Profile. This entity has a few fields (name (string), description (text), birthdate (date))... etc. And I want to add to this entity images and videos, so these would be shown in the user's profile. If it was just one image and one video to upload I would've used a field image (string) and video (text) or something like that. But knowing that I want the user to upload several of each, it won't work.
The images will be uploaded by the user in the form. And for the videos, he needs to paste an embeded tag from any major website.
How to do this please? I thought about creating a junction table for both an image entity and videos entity. But I'm not sure at all if that's the right way to do it either in general or in Symfony specifically.

You should consider the following approach:
- create tables for photos and videos (create entities Photo and Video if you are using Doctrine);
- add column userId to the corresponding tables (add this link property User to the corresponding entities);
- insert/update the corresponding tables (via entities) when a user uploads/edits a new photo or video.
Remember, you have many (in general) photos/videos for one user but only one user (owner) for each photo/video.
If you are using Doctrine this short tutorial about associations mapping will be useful for you:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

Related

Update many sub documents where match using PHP

I have a document of photos and on those photos I have sub array of arrComments. Each of the sub documents as a commenters profile picture (naturally there could be multiple comments on a single photo by the same user...)
When the user updates their profile picture, I want to update all of their profile picture references in all of the nested document comments for every picture.
I'm using PHP Mongodb driver for this btw.
I tried this:
...etc...updateMany(
array("arrComments.userOID"=>$userID),
array('$set'=>array('arrComments.$.userProfileImage'=>$arrFileName))
);
but that only updates the first found comment within each picture... I need ALL comments by a user on ALL images to be updated.

Laravel 4 - Model relations for Posts with multiple Image Uploads

How would you set this up?
Each Post has one Image (which can have a title/caption/etc) and each Image has multiple Uploads (different image sizes).
I've got a working setup for this going right now using pivot tables and model relations but it's not very pretty to query the data with these uploads... I just wanted to take a step back for a moment here and ask how another person out there would approach this?
I want to use eloquent to simply grab the Post Object with all of it's associated images and their uploads included.
Here's a screenshot of my full current db schema in case you're interested:

handling images, videos, docs for custom cms

I am in the process of building a CMS using php, however I am struggling on how to handle the various different assets that a dynamic page could serve, typically an average page will contain pictures of various sizes(thumb, body, main, etc), inline images and various different assets(pdf, flv, mp4, doc, etc).
At the moment a user can upload assets and create a folder on the server, I just wanted some techniques and concepts on how to manage this in terms of deleting, editing and linking to my dynamic pages within the cms? I already have a content table which contains all the content(meta_stuff, title, friendly_url, content_text, etc).
On upload should a reference be stored somewhere in say a asset table?
Should all paths to assets(images, docs, videos,etc) be stored in one table? or separate for each asset?
Should multiple image sizes(small, medium, large, etc) be stored in different fields e.g(assetid, smallpath, mediumpath, largepath?
What technique to use to link assets to the dynamic page? should this be a joined table or single? how do I go about retrieving the different assets for a page several pdfs and several images?
How to handle deleting of assets as this could be referenced to another dynamic page?
and anything else you think would be beneficial?
Thanks for all your help
Here are some basic things to keep in mind when dealing with resources (videos, images, documents..) in a scenario similar to what you're describing.
You'd want is to know what's being uploaded, what kind of stuff are being uploaded and who's uploading. To do that, it's good to have the following tables:
Edit: Sorry about the mistake, asset_types.asset_type_id should be linked to assets.asset_type_id and NOT assesst.asset_id
(Note: This is just an outline, of course you'll have more fields)
This model makes it easy to do the following tasks
Upload: When something is uploaded, you store it somewhere (with the correct
.htaccess configurations) then store that path in the table.
Delete: When the user is trying to delete something, you check if he's the owner.
When you want to add a new type, you don't need to create a new table.
Hopefully this will put you in the right direction.
Update: Answering your comment, when a user wants to add an image to an article for example, you have two options here:
The user is presented with a button/select menu/whatever to choose the desired image, once clicked it will add <img src=LINK_TO_RESOURCE" /> to the body of the content (Make sure you protect yourself from XSS).
OR
You use your own simple markup, when the user clicks on an image this [[PREFIX_IMG:IMG_ID]] will be added to the body of the content, when the content is viewed you'll replace that "tag" with <img src="LINK_TO_RESOURCE" /> which you've acquired using IMG_ID.
2nd Update: Well you have a couple of options to handle resources that aren't directly embedded in the "articles" themselves.
You can have a field in the "articles" table you can call summary for example, and one more column to the assets table which you can call asset_sub_type and have different types like summary. Then when you want to view the summary you fetch the summary text/title from database and you add the resource to it.
(This is the technique I use) When fetching the summary from the database I see which image is the database that is related to that article and the resize it on the fly with PHP and append it to the summary.

Writing you own media library: where to start?

i'd like to add a media library to a custom-made CMS that is build on Zend Framework and Doctrine. The goal is to build something like Worpress's media library: you can upload media and then attach it to for example an article.
Do you have any suggestions how to start on this? How should the database be designed? Is there any code that i can (re-)use to build this? Is there any literature that I should read on this subject? Thanks!!
I don't know how WPs library is built, but it's Open source, you can take a look.
As for table structure, assuming you want a many-to-many link you'd want a cross reference table, something like:
record_id (int)
media_id (int)
title (text)
caption (text)
rank (int)
Then the media table would be something like:
id (int)
title (text)
caption (text)
filename (text)
type (image|multimedia|document)
Perhaps you'd also add a folder_id field to the media table, or perhaps you instead want tagging, in which case you have a third table with media_id and tag as fields (or you have tags as comma separated values in the media table).
This will let you link one media to multiple items and one item to multiple media. It'll let you set a title and caption for the media item and override it for a specific link. For instance you have a picture of a house with the caption of whose house it is, but in one link the caption is overwritten (in the cross reference table) with text saying how this house is an example of a certain architecture. SQL's coalesce function will come in handy for getting the correct title and caption.
If the content that links to media comes from multiple table, say you have a table called 'staff' and another called 'products' and they can both link to media, then the cross reference table also needs to have a table_name field. The type field is so that you can easily get only the multimedia attached to a record, or only the images; you may want to count how many documents are attached, how many images etc, basing this off the filename on each query means you have slower queries.
One thing this doesn't quite cater for is hosting the media off site. If you use say Amazon S3 for storing these images, then the 'filename' field would actually be a URL to the image. I'm just shooting this out there as something you need to take into consideration when designing the media library.
I can't think of any literature or code you can copy-paste though, it's not difficult to do, though I do appreciate this can be time consuming.
Best of luck.
write a file uploader and file browser
that's all

How to store multile images full path into a database so that I can display them in a user home page like Orkut

I am making an application in which every user has to sign in first and then he can access his home page. Now on the home page, I have given an option of uploading an image. Now if the user is uploading one image I am storing the full path of the uploaded image into a database and from there I can display the image easily by an img tag...
But what should I do when the user want to upload many images? Then how should I store their full paths in a database for the same user. Give me an idea just like Orkut or Facebook. Should I make a different table with named images and should I store images in different rows with the same username. What should I do?
I don't know the logic. What should I do? How can I upload many images and how can I store their path and what will be the wisest method and how do I display many images on one page (I can display one)?
You can make a folder, named after user name and id and put all their images there.
To display many images on one page, just add more <img> tags to the page.
This seems to be more of a design question than a PHP question. I would create a separate table to store all paths, this is more normalized.
You still need to handle the UI, but if you are doing a sort of gallery then that is fairly simple with some jQuery sideshows or something like that.
Yes, you want to use a separate table to store the image paths. You'll most likely want a record ID, the User ID, and the path to the image. You could also add a field to contain the sorting order for the images.
Having the sorting order field will allow you to page through the photos if there is more than one page of photos.
Your thinking is correct where you suggest creating a separate table with rows containing the image path and the username. The concept that you are dealing with is called cardinality. I'd recommend that you take a few minutes to read about this concept, since it is so important to database design.
In this case, you're talking about a one-to-many relationship between the user and the images.

Categories