Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:33:47+00:00 2026-06-12T16:33:47+00:00

We are planning on using a tagging system similar to what is implemented on

  • 0

We are planning on using a tagging system similar to what is implemented on this very site.

We have the actual tagging front-end, and the autocomplete etc working.

But I am a but confused as to the best way to handle it on the back end.

Basically, when we get the tags on the backend we end up with an array that looks like:

array(
  array(
    'value' => 1,
    'label' => 'First Tag'
  ),
  array(
    'value' => 2,
    'label' => 'Second Tag'
  ),
  array(
    'value' => 'Third Tag',
    'label' => 'Third Tag'
  ),
  array(
    'value' => 3,
    'label' => 'Fourth Tag'
  ),
)

The tagging plugin also receives the same array format json_encode()‘d via ajax when it autocompletes, it displays the label, and stores the id so it can send it back.

So, tags with the values 1,2,3 are tags that were selected from the autocomplete.
The tag with the value of Third Tag is one that was not selected from the autocomplete, and may, or may not already exist in the database but has been typed in manually.

Now there is the change that a user could actually create a tag that happens to be a number, hence

array(
  'value' => 3,
  'label' => 3
)

Could come through, but not already exist, so we can’t just assume that if value is an int then it already exists.

So, the first part of this question is, how do I manage this so I do not end up with duplicate tags?

My current approach is, when when the tagging plugin requests tags via autocomplete I send back an array like

(term = ‘pin’)

array(
  array(
    'value' => '||1',
    'label' => 'pink'
  ),
  array(
    'value' => '||4',
    'label' => 'pin cushion'
  )
)

and then on the back end just assume that any tags that have a value starting with || have come from the autocomplete and already exist.

Then,
we query the database for all tags,
with the rest of the tags we check to find out if the value exists the array’s label keys, if it does we just leave it as is, if it doesn’t we create it, and then we switch out the value with the new id in the original array.

But that feels hacky to me, it means we are using a filler item (||) there must be a more elegant way of doing that?

The next part of the question is, actually linking these tags to an item.
This is more in context of editing a question on this site,

Some tags are already linked with a question. how do you handle it so that you don’t end up with duplicate tag references on a question?

I see two options so far:
Remove all links to tags from the question, then insert them all again.(2 queries)
or
Query the database for all tags connected to the question, loop over the array removing those tags from the array, then insert the remainder. (2 queries)

Is either method better than the other? or is there a third version?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:33:49+00:00Added an answer on June 12, 2026 at 4:33 pm

    Any kind of duplicate key question could be resolved at the DB level by adding a unique constraint on the relevant fields. All of your codes interactions with the tags should be done using the text label, which should serve as the unique identifier for the tag. Any type of numeric ID serves no purpose for the application itself and therefore there is no need for it to peek out from behind the repository layer. This would also address differentiating existing/new tags…effectively the application doesn’t care and it treats the tags as persisted Value Objects rather than worrying about any kind of Entity style life-cycle. In the repository call where the tag is associated with the article create the tag if it is not already present. The ID will primarily benefit performance wise when doing the JOIN(s) needed for tag queries (and really doesn’t anywhere else), which is again something the application shouldn’t care about outside of the repository which does the join.

    The safest and simplest bet for updating tags, including deletions, would be to blast out the existing tags and write new ones. This ensures that the persisted state matches the UI input completely and consistently, and realistically this would not be an expensive operation, nor would it be performed often enough to care about (though a simple programmatic check to see whether an update is needed would help prevent needless writes). It’s 2 queries that should be wrapped in a transaction and could be batched together, and the DELETE in particular should be very cheap so long as the proper index is in place, so its not the kind of multiple query you need to worry about.

    If for some bizarre reason you were overly anxious on minimizing the work of the database, you could store a version of the tags beforehand and then afterwards and do the queries appropriate for the delta, but this is far more fragile, and could also introduce many complex concurrency concerns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Planning of using t4 template in a production environment. I have not used this
So I have this layout and I'm planning on using javascript/jquery to change the
Soon I'll be launching my new site and i was planning on using gmail
Planning to create a new website for our product using ASP.NET MVC 4. Site
I'm planning on using Magento's inbuilt Custom Variable system to allow users to alter
Am planning on using declarative authorization in a Rails 3 app. I have the
Am planning on using declarative authorization in a Rails 3 app. I have the
I've been planning on using GIT for version control. However, I have a prolly
We are planning on using Red Gate SQL Source Control , but have not
I'm planning on creating a view using this SQL SELECT, but the explain for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.