DDC-2528: Extracting entities through DQL query resets the previous associations #3171

Open
opened 2026-01-22 14:14:29 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 24, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user legolas:

I have the entities Purchase(owning side) and Product(inverse side) sharing bidirectional many to many relation. Now whatever associations I build between product and purchase entity, those associations are erased if by mistake I happen to run a join query between the products and purchase table. For example look at this code.

$purc = $em->find("\ZC\Entity\Purchase", 1); //Existing purchase entity
//Now this purchase entity is linked to 2 products through the join table
$prod = new \ZC\Entity\Product();
$prod->name= "newly";
$purc->addProduct($prod); //Added a new product to the collection
// print(count($purc->products));
$dql = "SELECT b,c FROM \ZC\Entity\Purchase b join b.products c where      b.id = 1";
$query = $this->entityManager->createQuery($dql);
$purc2 = $query->getResult()[0]; //Now this is same as $purc
print(count($purc->products));   //again prints 2

As you can see, when I have added a new entity $prod to the $purc, the number of products linked to $purc should have increased to 3. It seems the join overrides the previous associations formed. More strange is if I had fetched the products related to $purc from the tables before the join query(the commented code before dql), then the last print statement would have given 3.
I am facing a lot of similar issues.

Originally created by @doctrinebot on GitHub (Jun 24, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user legolas: I have the entities Purchase(owning side) and Product(inverse side) sharing bidirectional many to many relation. Now whatever associations I build between product and purchase entity, those associations are erased if by mistake I happen to run a join query between the products and purchase table. For example look at this code. ```php $purc = $em->find("\ZC\Entity\Purchase", 1); //Existing purchase entity //Now this purchase entity is linked to 2 products through the join table $prod = new \ZC\Entity\Product(); $prod->name= "newly"; $purc->addProduct($prod); //Added a new product to the collection // print(count($purc->products)); $dql = "SELECT b,c FROM \ZC\Entity\Purchase b join b.products c where b.id = 1"; $query = $this->entityManager->createQuery($dql); $purc2 = $query->getResult()[0]; //Now this is same as $purc print(count($purc->products)); //again prints 2 ``` As you can see, when I have added a new entity `$prod` to the `$purc`, the number of products linked to `$purc` should have increased to 3. It seems the join overrides the previous associations formed. More strange is if I had fetched the products related to `$purc` from the tables before the join query(the commented code before dql), then the last print statement would have given 3. I am facing a lot of similar issues.
admin added the Bug label 2026-01-22 14:14:29 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3171