The website I'm building has a table which stores all the information of uploaded images on the site. These uploaded images can come from different resources such as a guestbook, news section or an item from an agenda.

Ofcourse I want the image to inherit the rights of the resource it is part of. For example: if user A isn't allowed to view the guestbook I don't want him to be able to view an image posted on the guestbook by going to image/view/id/12 (which would be the image request used it in the guestbook).

What I have now is that the system remembers the resources used (in this case the guestbook) the image-id is coupled to the resource-id. However I don't know to which guestbook post the image is connected (I do ofcourse know it the other way around).

***Is there a way in SQL to connect one table field to a field in another table, where which table I connect to can vary based on one of the first table's field values?*** 

In my case I would like to connect an image to a resource this could be a guestbook post in the table gb\_posts or an agenda item in the table agenda\_items.

Or is this all a stupid way of solving the problem and should I not use one table for the uploaded images but keep the image attached to the resource (as a column in the table for example)? It sounds like using one table is at least a lot slower in use (but I would have a great overview of all the images in one place).

I hope you guys can help me out.

**EDIT: extra explanation: db model**
I will try to explain how it all works the best I can.

First of all: I use Zend Framework, and therefor I also use Zend\_Acl for working with priveleges.

My DB structure:
- Users are connected to roles (directly or by being connected to a group that is connected to a role)
- There is a table resources containing all the resources which is connected to priveleges. For example: guestbook is a resource, view or edit are the priveleges. Next to the controllers/actions there can also be other resources in this table such as a category within the agenda or a file location.
- roles are connected to a privelege

When for example the guestbook is requested for viewing I can check if the user is allowed to.

In short something like:
users -> roles -> priveleges <- resources

When a user adds a guestbook post with an image, the used resources (in this case guestbook is saved):
guestbook\_posts -> images -> resources

I hope this explains my DB model for a bit, if it doesn't I will try to create an image of the tables.