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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:14:04+00:00 2026-05-27T14:14:04+00:00

I have an User entity. And those users can be friends together. So I

  • 0

I have an User entity. And those users can be friends together. So I defined a self referencing manyToMany unidirectional association (because there is always reciprocity is friendship, right ?).

a piece of my user entity in YML

manyToMany:
    friendList:
    targetEntity: User
    joinTable:
      name: user_friend
      joinColumns:
        user_id:
          referencedColumnName: id
      inverseJoinColumns:
        friend_id:
          referencedColumnName: id
    cascade: [persist]

When I call $user->addFriendList($friend), and after a persist and a flush, I have PDOException:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘1-2’ for fey ‘PRIMARY’

When I check in the logs, I can see that doctrine is trying to exectue the same insert query twice.

For your information, my addFriendList function

public function addFriendList(User $friend)
{
    if (!$this->friendList->contains($friend)) {
        $this->friendList[] = $friend;
        $friend->addFriendList($this);
    }
}

Where am I wrong here ?

  • 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-27T14:14:05+00:00Added an answer on May 27, 2026 at 2:14 pm

    I finally found a solution to my issue. However I still don’t know if it’s a Doctrine2 defect or if it works as designed.

    I need to persist my user and flush before adding friends.

    So my working code is:

    $em->persist($user);
    $em-flush();
    
    $user->addFriendList($friend);
    $em->persist($user);
    $em->flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, user and role, defined as: public class User : Entity
In our application, I have seen code written like this: User.java (User entity) public
I have a 'vanilla' install of CodeIgniter 2 + Doctrine 2. My tutorial Entity.User.dcm.yml
it's possible to have one entity object (class, e.g. User) for more entity models
I have an entity loaded by Hibernate (via EntityManager ): User u = em.load(User.class,
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
Background: I have a website (ASP.NET MVC) where users can write/edit/delete a review. Doing
I have a User entity that contains the username, name and other non sensitive
I have a certain entity that can possibly be edited by different actors. A
When you have an entity, such as UserEntity, who's id property is derived from

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.