开发者

Choose a database table to join with, using a field value

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

开发者_如何学GoI hope this explains my DB model for a bit, if it doesn't I will try to create an image of the tables.


I have to admit I'm failing to completely understand the model you wish to implement, but there is an interesting quote...

However I don't know to which guestbook post the image is connected (I do ofcourse know it the other way around).

If you know an association one way, you should be able to use the associaton in both directions? I'm assuming you have a table that includes "post_id, image_id", or something?

It may be that the table is only indexed post_id first, in which case querying that table by image_id may be slow, but then you can just include a new index with image_id first?


If you can give examples of the table structure you have at present, and an example of the query you can't fullfil, we may be able to help you further.


Sounds like you want a foreign key constraint.

Update: Completely misunderstood the question, apparently.

There are two approaches here:

  1. As it currently stands, there is nothing in the schema that would prohibit linking the same image from multiple resources. If that is desired, then a foreign key constraint and an index for the backreference is probably the best solution, although it will not scale well, and requires additional computation (because the rights on the image need to be the union of the rights of the refering resources).

  2. The alternative is to create some kind of inheritance schema, where there is a table listing "resources" (that effectively just contains identifiers) that is referenced as a foreign key from the actual resource tables and the images table; the only constraint that cannot be expressed in plain SQL is that different resources may not share the same identifier.


Create two SELECT clauses, each having the correct joins to the correct tables, and then combine the output of the two SELECT clauses together using a UNION statement.

SELECT field1, field2 
FROM table1 
JOIN table2 on table1.PK = table2.FK
WHERE table1.selector = 1

UNION SELECT field1, field2 
FROM table1 
JOIN table3 on table1.PK = table3.FK
WHERE table1.selector = 2    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜