views:

43

answers:

3

I'm building an app where each user can have multiple profiles. Users can upload multiple photos for each profile, etc.

The folder structure will be something like

public_html/
upload/
    user-123/
        profile-199/
        profile-321/
            images/
                123423.png

I'm going to have a one to many relationship between each profile and each photo, but I'm trying to see how I should flag a photo as special and the users "profile photo".

My proposed table structure is something like this

id
profile_id
filename  (or maybe extension only, since filename will most likely just be the primary id for that photo)
profile (boolean field of whether this a profile pic or not)
date_added

How should I treat special images, like profile pictures?

Edit: Sorry if I wasnt clear. I want each user to be able to have multiple profiles, each profile to have many photos. Just like for Facebook, you can have many photos, but only one profile pic at any time.

A: 

That would work but it would have a problem that more than one photo could be marked as a profile photo unless you take special care to prevent this.

An alternative design is to have a table with columns user_id and special_photo_id where the photo_id is a foreign key into the photos table. This has a slightly different issue: now it is possible to have a special photo that doesn't belong to the user.

Mark Byers
As you were writing this, I revised my answer and said I should probably put a unique index on profile_id and profile :)
BDuelz
@BDuelz: Then you couldn't have multiple rows with "profile = false" for the same user. It would work if you used TRUE and NULL though since you can have multiple NULLs even in a column that has a unique index.
Mark Byers
For option 2, how would I do that. Currently, I have it so each profile can have many photos, If I added a photo_id column to the profiles table, then it would imply that each profile can only have 1
BDuelz
@Mark. Whoooops. In the quickness of things, that totally slipped my mind
BDuelz
When you wrote "photo as special and the users "profile photo" that also implied that there was only one such special photo. How many special photos can there be?
Mark Byers
@BDuelz: Now I am confused as to what you want. :-s
Mark Byers
for twitter, every user can only have one photo at any time (I think). facebook, on the other hand, allows you to have many photos and then choose which one you want to be your profile pic. I'm looking for something like the facebook approach
BDuelz
@BDuelz: My second approach should do that. I haven't removed anything from your design apart from the profile column, so everything you could do before, you can still do. I just added one extra table to connect a user to the special photo.
Mark Byers
I see now... straying off topic a little bit, would you mind providing feedback on my proposed folder structure, and the thought of me storing the filename vs the file extension in the photos table. Thanks
BDuelz
Your storing the extension only will work. What about also storing the original filename that the file had when the user uploaded it? This might be useful for the user to be able to see.
Mark Byers
One problem with naming images with ids is that anyone can write a script to extract all the images. If you don't care about that, saving images as <id>.<ext> will work.
gsharma
A: 

You could have a field in your users table(where its username and other info is stored) with a profile_id field init pointing to the profiles table. That way you get one possibility for the profile and away to identify it.

Thinking about it that will only work if you do have a users table and a separated profiles table.

Iznogood
I'm sorry, but I dont understand. Are you saying to put a profile_id in the users table? And if so, then wouldnt that imply that each user can only have one profile?
BDuelz
Well yes and no. My idea is to just put in the users table wich profile is the right one. You still keep the other relation 1 to many with the other profiles. My answer adresses finding out wich profile is the main one. Not how to manage all the profiles. You already do that.
Iznogood
Oh... I get it. Good idea
BDuelz
A: 

How about the image table looks like this:

id
profile_id
filename    
date_added

and add the column 'profile_image_id' in the profile table? This will be your special image's id.

I imagine you are going to have a lot more non-special images than special. So, most of the time the field 'profile(boolean)' that you proposed will be 0.

Edit: I believe @Iznogood is saying the same thing.

gsharma