Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 insists on persisting already managed entity in ManyToOne relationship

I have a database table containing departments. I have another table containing people. As you'd expect a department contains many people, and a person is in a single department.

When I want to persist a new person to the database, I create a Person object and try to set it's Department property to an existing Department object which is managed by the Entity Manager. However, when I try to persist my new Person, I get an exception:

A new entity was found through the relationship 'Entities\Person#department' that was not configured to cascade persist operations for entity: Entities\Department@0000000016abe202000000000d29dd37. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

I don't fully understand the part of the exception which says the department is an "unknown entity", since I extracted it via the Entity manager.

As the exception suggests, I inserted a cascade into the yml metadata (cascade: ["persist"]). My person then gets saved, but I end up with a duplicated Department in the departments table, with a new id.

This must be a very common use case. I've included my code and metadata below. What changes should I make?

Metadata:

Entities\Person
  type: entity
  table: people
  fields:
    ...
    departmentId:
      type: integer
      unsigned: false
      nullable: false
      column: department_id
    ...
  manyToOne:
    department:
      targetEntity: Entities\Department
      joinColumn: department_id
      referenceColumnName: id

Code:

$department = $em->getRepository('Department')->findOneBy(array('name' => $departmentName);

$person = new Person();
$person->setName('Joe Bloggs');
$person->setDepartment($department);

$em->persist($person);
$em->flush();
like image 395
Michael Avatar asked Aug 16 '12 11:08

Michael


People also ask

What is a manytoone relationship?

But for relationships, we use @ORM\ManyToOne, @ORM\ManyToMany or @ORM\OneToMany. This is a ManyToOne relationship because many events may have the same one User. I’ll talk about the other 2 relationships later ( OneToMany , ManyToMany ).

Can a blog post have multiple categories in doctrine 2?

This relationship means that a blog post can have multiple categories. And categories can be used in multiple blog posts. As you can see we need a junction table for this. This junction tables links the blog posts with the correct categories. So how is the simplest and most correct way to do this in Doctrine 2 with entities?

How do you create a relationship in doctrine?

In Doctrine, relationships are handled by creating links between objects. Start by creating an owner property inside Event: For normal fields, we use the @ORM\Column annotation. But for relationships, we use @ORM\ManyToOne, @ORM\ManyToMany or @ORM\OneToMany.

What is a unidirectional many-to-many Association?

From Doctrine's point of view, it is simply mapped as a unidirectional many-to-many whereby a unique constraint on one of the join columns enforces the one-to-many cardinality. The following example sets up such a unidirectional one-to-many association:


1 Answers

The problem was caused by using different instances of the Entity Manager to first fetch the department, and then persist the Person.

My entity manager is now a singleton, so whichever class requests an entity manager gets the same instance.

like image 96
Michael Avatar answered Nov 09 '22 08:11

Michael