Skip to content

Commit 91053aa

Browse files
Zacharias Luitenzluiten
Zacharias Luiten
authored andcommitted
Fix #4252 Cascade first before refreshing the entity itself so toMany associations are not reset to empty collections
1 parent 676a1bb commit 91053aa

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/Doctrine/ORM/UnitOfWork.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2216,12 +2216,12 @@ private function doRefresh($entity, array &$visited): void
22162216
throw ORMInvalidArgumentException::entityNotManaged($entity);
22172217
}
22182218

2219+
$this->cascadeRefresh($entity, $visited);
2220+
22192221
$this->getEntityPersister($class->name)->refresh(
22202222
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
22212223
$entity
22222224
);
2223-
2224-
$this->cascadeRefresh($entity, $visited);
22252225
}
22262226

22272227
/**

tests/Doctrine/Tests/ORM/Functional/Ticket/GH4252Test.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Doctrine\Tests\ORM\Functional\Ticket;
46

57
use Doctrine\Common\Collections\ArrayCollection;
68
use Doctrine\Common\Collections\Collection;
9+
use Doctrine\ORM\Mapping\Column;
10+
use Doctrine\ORM\Mapping\Entity;
11+
use Doctrine\ORM\Mapping\GeneratedValue;
12+
use Doctrine\ORM\Mapping\Id;
13+
use Doctrine\ORM\Mapping\ManyToOne;
14+
use Doctrine\ORM\Mapping\ManyToMany;
15+
use Doctrine\ORM\Mapping\OneToMany;
16+
use Doctrine\Tests\OrmFunctionalTestCase;
717

818
/**
919
* @group GH-4252
1020
*/
11-
class GH4252Test extends \Doctrine\Tests\OrmFunctionalTestCase
21+
class GH4252Test extends OrmFunctionalTestCase
1222
{
13-
protected function setUp()
23+
protected function setUp() : void
1424
{
1525
parent::setUp();
1626

17-
$this->_schemaTool->createSchema(array(
27+
$this->_schemaTool->createSchema([
1828
$this->_em->getClassMetadata(GH4252City::class),
1929
$this->_em->getClassMetadata(GH4252Resident::class),
2030
$this->_em->getClassMetadata(GH4252Address::class),
21-
));
31+
]);
2232
}
2333

2434
public function testIssue()
@@ -42,7 +52,7 @@ public function testIssue()
4252
$this->_em->refresh($city);
4353

4454
$resident = $city->getResidents()->first();
45-
$address = $resident->getAddresses()->first();
55+
$address = $resident->getAddresses()->first();
4656

4757
$this->assertTrue($city->getFlag());
4858
$this->assertTrue($resident->getFlag());
@@ -57,7 +67,9 @@ class GH4252City
5767
{
5868
/**
5969
* @var int
60-
* @Id @Column(type="integer") @GeneratedValue
70+
* @Id
71+
* @Column(type="integer")
72+
* @GeneratedValue
6173
*/
6274
private $id;
6375

@@ -69,7 +81,6 @@ class GH4252City
6981

7082
/**
7183
* @var GH4252Resident[]|Collection
72-
*
7384
* @OneToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH4252Resident", mappedBy="city", cascade={"persist","refresh"})
7485
*/
7586
private $residents;
@@ -130,7 +141,6 @@ class GH4252Resident
130141

131142
/**
132143
* @var GH4252Address[]|Collection
133-
*
134144
* @ManyToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH4252Address", fetch="EXTRA_LAZY", cascade={"persist","refresh"})
135145
*/
136146
private $addresses;
@@ -180,7 +190,9 @@ class GH4252Address
180190
{
181191
/**
182192
* @var int
183-
* @Id @Column(type="integer") @GeneratedValue
193+
* @Id
194+
* @Column(type="integer")
195+
* @GeneratedValue
184196
*/
185197
private $id;
186198

0 commit comments

Comments
 (0)