Saving Tags in the Database
I am trying to implement tags in my application.
tags(id, name)
tags_relations (id, tag_id开发者_运维问答, entity_id)
entity (id, name)
here i have a set of 4 tags which i wanna show it as a catgory
Dish type -> Breakfast, Lunch, Dinner, Snacks
which are actually tags which can be checked or unchecked for a particular entity.
In another field i allow users to enter tags comma seperated.
I just want to knw which is the best way to save it into the database ?
I am using CakePHP framework..
to follow cake standard:
tags(id, name) tags_entities (id, tag_id, entity_id) entities (id, name)
bake the 3 tables again and cake will automatically assign the correct relationships. The auto-generated code will let you check and uncheck tags. If you want to "allow users to enter tags comma seperated", you'll need to write some more code to handle that.
the join table should be named entities_tags
:
join table's name needs to include the names of both models involved, in alphabetical order, and separated with an underscore ( _ )
精彩评论