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 9193115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:05:04+00:00 2026-06-17T21:05:04+00:00

I have a scenario where I’m updating a blog post which has multiple tags

  • 0

I have a scenario where I’m updating a blog post which has multiple tags assigned to it via a many to many relationship (in a link table called blog_link_tags) as below:

...

$em = $this->getDoctrine()->getManager();
$blogPost = $em->getRepository('MyBlogBundle:Blog')->find($postId);


$blogPost
    ->setTitle( $request->request->get('post_title', '') )
    ->setBody( $request->request->get('post_body', '') )
    ->setLive(true);


$postTags = json_decode( $request->request->get('post_tags', '') );
$tagRepository = $em->getRepository('MyBlogBundle:BlogTag');

foreach($postTags as $postTag) {
    $tag = $tagRepository->find( $postTag->id );
    if (!$tag) {
        throw $this->createNotFoundException('Tag not found: ' . $tag->title);
    }
    $blogPost->addTag($tag);
}

$em->flush();

....

As you can probably tell though, if I edit a blog post and add a new tag then it will create duplicates records.

What is the best way to either truncate the blog_link_tag table of records of the current blog post id, OR only insert those tag ids that are unique? Would it be to do it on the following line within the foreach loop:

$tag = $tagRepository->find( $postTag->id );

But instead check to see if the tag existed AND if it was not yet present in the link table? Or does Doctrine 2 offer a better way to achieve such an action?

  • 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-17T21:05:05+00:00Added an answer on June 17, 2026 at 9:05 pm

    As far as I’m concerned I would first create a FormType for the Post entity so that I don’t have to write any messy code into the controller and everything is automated, clean and secure.

    Next, I would manage the tags within the Post entity as in:

    class Post
    {
    
        private $tags;
    
        public function __construct()
        {
            $this->tags = new ArrayCollection();
        }
    
        public function setTags($tags)
        {
            $this->tags->clear();
    
            foreach ($tags as $tag) {
                $this->addTag($tag);
            }
    
            return $this;
        }
    
        public function addTag(Tag $tag)
        {
             if (!$this->tags->contains($tag)) {
                 $this->tags[] = $tag;
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario in which I have to run multiple commands one after
I have a scenario where I'm doing a post in Sinatra via Typhoeus in
I have a scenario where my software has to work even when there is
I have a scenario with a domain model used with NHibernate which looks kind
I have following scenario, for which i need to write sql query. I have
I have a scenario in which I need to run all template files under
I have a scenario which is not easy to explain, but I am sure
I have following scenario: alt text http://static.zooomr.com/images/7579022_e64808b855_o.png We have a WebService which poses as
I have scenario, in which I am getting images using Web Service and all
I have a scenario where I'm coding a ScrollView which is intended to act

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.