Skip to content

Commit

Permalink
Fix #4252 Cascade first before refreshing the entity itself so toMany…
Browse files Browse the repository at this point in the history
… associations are not reset to empty collections
  • Loading branch information
Zacharias Luiten authored and zluiten committed Apr 24, 2022
1 parent 676a1bb commit 3b12f46
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2216,12 +2216,12 @@ private function doRefresh($entity, array &$visited): void
throw ORMInvalidArgumentException::entityNotManaged($entity);
}

$this->cascadeRefresh($entity, $visited);

$this->getEntityPersister($class->name)->refresh(
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
$entity
);

$this->cascadeRefresh($entity, $visited);
}

/**
Expand Down
15 changes: 8 additions & 7 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH4252Test.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group GH-4252
*/
class GH4252Test extends \Doctrine\Tests\OrmFunctionalTestCase
class GH4252Test extends OrmFunctionalTestCase
{
protected function setUp()
protected function setUp() : void
{
parent::setUp();

$this->_schemaTool->createSchema(array(
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(GH4252City::class),
$this->_em->getClassMetadata(GH4252Resident::class),
$this->_em->getClassMetadata(GH4252Address::class),
));
]);
}

public function testIssue()
Expand All @@ -42,7 +45,7 @@ public function testIssue()
$this->_em->refresh($city);

$resident = $city->getResidents()->first();
$address = $resident->getAddresses()->first();
$address = $resident->getAddresses()->first();

$this->assertTrue($city->getFlag());
$this->assertTrue($resident->getFlag());
Expand All @@ -69,7 +72,6 @@ class GH4252City

/**
* @var GH4252Resident[]|Collection
*
* @OneToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH4252Resident", mappedBy="city", cascade={"persist","refresh"})
*/
private $residents;
Expand Down Expand Up @@ -130,7 +132,6 @@ class GH4252Resident

/**
* @var GH4252Address[]|Collection
*
* @ManyToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH4252Address", fetch="EXTRA_LAZY", cascade={"persist","refresh"})
*/
private $addresses;
Expand Down

0 comments on commit 3b12f46

Please sign in to comment.