With only a bit of previous experience with databases and no formal education with them, I’m a bit stuck as to how to model this (and retrieve the data I require from it in PHP). This is what I’m trying to model:
For each item on my site, it is allowed to have multiple tags such as file, upload, php, recursive etc. However the tags are reusable, I can have two different items and they each could have the php tag.
I’ve been trying to read up on how to do this and whether it is lack of experience with it or something else I don’t know, but I can’t seem to grasp the concept. Apparently you need a middle table which links the two together?
Also once I have this relationship and the tables defined, how would I do things such as:
– Retrieve all items with a certain tag?
– Retrieve all tags that one item has?
Thanks for your help, also if anyone could list any further reading on this to strengthen my understanding of the concept that would be great.
The db part is easy. This is just a sample so you can see how db can look like, not any particular SQL engine queries.
To get items with
yourTagtag you will just run query like thisTo get tags associated with post with id of
123you run this query:For the PHP part you could use a framework. Many (if not all) frameworks can easily model such relationships. For example in CakePHP this done like that: