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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:32:12+00:00 2026-06-18T17:32:12+00:00

Background: In my application I have an entity that has a self referencing ManyToOne

  • 0

Background:

In my application I have an entity that has a self referencing ManyToOne association (many children can point to a single parent). And I have a feature that does mass updates on many entities at one time using the Doctrine ORM. To keep performance from dropping dramatically due to many entities being loaded I detach entities once they’ve been updated.

Problem:

When I detach an entity that has children and later try to update any of those children Doctrine complains that it doesn’t know the parent anymore. Even if I merge the parent entity before trying to update the child.

Question:

What am I doing wrong when I detach the parent entity? I’ve tried doing cascade=”merge” and/or “detach” on the parent column and Doctrine still complains about the parent being an unknown entity when I try to persist.

I’ve mocked up a simple example that reproduces this. See below.

Test Code:

Entity\Thing.php

/**
 * @ORM\Entity()
 * @ORM\Table(name="things")
 */
class Thing
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Thing", inversedBy="children", cascade={"detach","merge"})
     * @ORM\JoinColumn(name="parentId", referencedColumnName="id", onDelete="SET NULL")
     */
    protected $parent;

    /**
     * @ORM\OneToMany(targetEntity="Thing", mappedBy="parent")
     */
    protected $children;

    /**
     * @ORM\Column(type="string", length=64)
     */
    protected $name;

    public function __construct($name = null)
    {
        $this->children = new ArrayCollection();
        $this->name = $name;
    }

    // .. SNIP ...
}

Test Action:

public function testThingAction($_route)
{
    $em = $this->getDoctrine()->getEntityManager();
    $repo = $em->getRepository('AcmeThingBundle:Thing');

    // simple setup of a couple things in the DB
    $t1 = $repo->findByName('Thing1');
    if (!$t1) {
        $t1 = new Thing('Thing1');
        $t2 = new Thing('Thing2');
        $t2->setParent($t1);

        $em->persist($t1);
        $em->persist($t2);
        $em->flush();
        return $this->redirect($this->generateUrl($_route));
    }

    list($t1, $t2) = $repo->findAll();

    // detach and re-merge Thing1
    // This should cause Thing1 to be removed and then re-added 
    // to the doctrine's known entities; but it doesn't!?
    $em->detach($t1);
    $em->merge($t1);

    // try to update T2
    $t2->setName('Thing2 - ' . time());
    $em->persist($t2);
    // will fail with: 
    // A new entity was found through the relationship Thing#parent
    $em->flush();

    return array();
}
  • 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-18T17:32:13+00:00Added an answer on June 18, 2026 at 5:32 pm

    The issue is that the child has a relationship to a specific parent object that is no longer managed by Doctrine. When you call $entityManager->merge($entity) you get a new managed entity back from that function.

    When you get that back, you need to manually call setParent() on each of your children with the newly managed entity.

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

Sidebar

Related Questions

Background: I have an application with athletes, each of these athletes (Athlete model) can
Background: I have an application that, when installed, generates a random identifier (AppId). When
Background info: I have a windows application (in c#) that handles a custom file
Some background: we have a windows application (c#) that locate in the system try.
Scenario I have a background worker in my application that runs off and does
I have a Java application that run as a background service, i.e. no GUI.
I'm developing a Rails application that has a background process that updates some user
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
BACKGROUND: I have application server logic that hosts stateful object instances. I plan to
I have application that runs non stop in the background on a dedicated remote

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.