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

  • Home
  • SEARCH
  • 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 7650551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:16:50+00:00 2026-05-31T11:16:50+00:00

I use Doctrine2 as ORM in a Symfony2 project. I have article and tag

  • 0

I use Doctrine2 as ORM in a Symfony2 project. I have article and tag entity classes. There are many-to-many relations between them. An article may have many tags and vice versa. There is a joined table, article_tags which just have 2 columns article_id and tag_id, and generated by doctrine. Everything works fine, except, when new article with some tags is added, both new and pre-existing tags are inserted into tag table in which I want to only store unique tags; the article_tags should just relate them.

My article schema:

  manyToMany:
    tags:
      targetEntity: Tag
      cascade: ["persist"]
      joinTable:
    name: article_tag
    joinColumns:
      article_id:
        referencedColumnName: id
    inverseJoinColumns:
      tag_id:
        referencedColumnName: id

Adding unique attribute to tag and ignoring duplicates is not reasonable since there should be relation. I tried to add logic that checks if the tag exists fetch tag data and add it to article:

$article->addTag($existingTag);

It gives the following error:

Catchable Fatal Error: Argument 1 passed to
Seferov\ArticleBundle\Entity\Article::addTag() must be an instance of
Seferov\ArticleBundle\Entity\Tag, null given, called in
/var/www/seferov.local/src/Seferov/ArticleBundle/Repository/TagRepository.php
on line 34 and defined in
/var/www/seferov.local/src/Seferov/ArticleBundle/Entity/Article.php
line 393

  • 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-05-31T11:16:51+00:00Added an answer on May 31, 2026 at 11:16 am

    You basically need to check if the tag exists. If it does then use it. If not then create one.

    public function loadOrg($orgId,$autoCreate = false)
    {
        $org = $this->getEntityManager()->find('ZaysoCoreBundle:Org',$orgId);
        if ($org) return $org;
    
        if (!$autoCreate) return null;
        if (!$orgId)      return null;
    
        $org = new Org();
        $org->setId($orgId);
        $this->getEntityManager()->persist($org);
        return $org;
    }
    

    Be sure to make your tag column unique and to catch exceptions. Possibility that another request might insert the same tag in between the find and the persist.

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

Sidebar

Related Questions

I'm learning Symfony2 and I use Doctrine2 as ORM. Everything went fine except one
Entities in symfony 2 have doctrine tags that are preceded with @ORM\ (eg @ORM\Entity).
i use two classes: namespace Test; use Doctrine\ORM\Mapping as ORM; /** *@Table() *@InheritanceType(Joined) *@DiscriminatorMap({baseclass=BaseClass,
I have recently started setting up the security for my Symfony2 project. I opted
I have multiple symfony2 applications which share common entities, but use different database settings.
I can't figure out how i can have an entity relationship between 2 bundles.
All my Doctrine2 setups are done within YAML files. I have an entity class
What do you think, should I use ORM like Propel or Doctrine? For a
I am tryign to use Doctrine_Pager for the first time. So I have the
So, I'm trying to use Doctrine to retrieve some data. I have some basic

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.