lundi 4 avril 2016

Correct way to process 1 to many and many to 1 with visual input of the type select2 (multi)

Having the following tables:

  • Post (Id, Title, Text, etc...)
  • PostTag(PostId, TagId)
  • Tag (Id, Name)

Now the edit form (internet solution) displays the "tags" associated to a post by using the Jquery Select2 control that allows multi entries. This input field returns a list of tag Ids.

I think the correct way to process this entry is:

  • array loop with an insert statement with not exists condition
  • and in the end one delete statement with not exists (to delete the tags that no longer exists).

Or is better to delete all and then insert the new list?

And how about when the table PostTag has the fields (Id, PostId, TagId, CreatedOn) that allows repeated entries? I believe that any of the two previous ways are corrected..... and all because the select2 control only return a list of tag Ids.....

My question applies to the same logic used here in stackoverflow Questions where we need to insert the tags....

What is the correct logic to use for this type of data?




Aucun commentaire:

Enregistrer un commentaire