Skip to content

Commit 486e406

Browse files
authored
Merge pull request doctrine#10065 from goetas/lazy-eager-collection-refresh
Lazy and eager collection refresh inconsistency
2 parents d9aa6ef + 7d1b24f commit 486e406

File tree

2 files changed

+163
-2
lines changed

2 files changed

+163
-2
lines changed

src/UnitOfWork.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,13 +2473,13 @@ private function doRefresh($entity, array &$visited, ?int $lockMode = null): voi
24732473
throw ORMInvalidArgumentException::entityNotManaged($entity);
24742474
}
24752475

2476+
$this->cascadeRefresh($entity, $visited, $lockMode);
2477+
24762478
$this->getEntityPersister($class->name)->refresh(
24772479
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
24782480
$entity,
24792481
$lockMode
24802482
);
2481-
2482-
$this->cascadeRefresh($entity, $visited, $lockMode);
24832483
}
24842484

24852485
/**
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Doctrine\ORM\Mapping as ORM;
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\Tests\OrmFunctionalTestCase;
14+
15+
class LazyEagerCollectionTest extends OrmFunctionalTestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->createSchemaForModels(
22+
LazyEagerCollectionUser::class,
23+
LazyEagerCollectionAddress::class,
24+
LazyEagerCollectionPhone::class
25+
);
26+
}
27+
28+
public function testRefreshRefreshesBothLazyAndEagerCollections(): void
29+
{
30+
$user = new LazyEagerCollectionUser();
31+
$user->data = 'Guilherme';
32+
33+
$ph = new LazyEagerCollectionPhone();
34+
$ph->data = '12345';
35+
$user->addPhone($ph);
36+
37+
$ad = new LazyEagerCollectionAddress();
38+
$ad->data = '6789';
39+
$user->addAddress($ad);
40+
41+
$this->_em->persist($user);
42+
$this->_em->persist($ad);
43+
$this->_em->persist($ph);
44+
$this->_em->flush();
45+
$this->_em->clear();
46+
47+
$user = $this->_em->find(LazyEagerCollectionUser::class, $user->id);
48+
$ph = $user->phones[0];
49+
$ad = $user->addresses[0];
50+
51+
$ph->data = 'abc';
52+
$ad->data = 'def';
53+
54+
$this->_em->refresh($user);
55+
56+
self::assertSame('12345', $ph->data);
57+
self::assertSame('6789', $ad->data);
58+
}
59+
}
60+
61+
/**
62+
* @Entity
63+
*/
64+
class LazyEagerCollectionUser
65+
{
66+
/**
67+
* @var int
68+
* @Id
69+
* @Column(type="integer")
70+
* @GeneratedValue(strategy="AUTO")
71+
*/
72+
public $id;
73+
74+
/**
75+
* @var string
76+
* @Column(type="string", length=255)
77+
*/
78+
public $data;
79+
80+
/**
81+
* @ORM\OneToMany(targetEntity="LazyEagerCollectionPhone", cascade={"refresh"}, fetch="EAGER", mappedBy="user")
82+
*
83+
* @var LazyEagerCollectionPhone[]
84+
*/
85+
public $phones;
86+
87+
/**
88+
* @ORM\OneToMany(targetEntity="LazyEagerCollectionAddress", cascade={"refresh"}, mappedBy="user")
89+
*
90+
* @var LazyEagerCollectionAddress[]
91+
*/
92+
public $addresses;
93+
94+
public function __construct()
95+
{
96+
$this->addresses = new ArrayCollection();
97+
$this->phones = new ArrayCollection();
98+
}
99+
100+
public function addPhone(LazyEagerCollectionPhone $phone): void
101+
{
102+
$phone->user = $this;
103+
$this->phones[] = $phone;
104+
}
105+
106+
public function addAddress(LazyEagerCollectionAddress $address): void
107+
{
108+
$address->user = $this;
109+
$this->addresses[] = $address;
110+
}
111+
}
112+
113+
/** @Entity */
114+
class LazyEagerCollectionPhone
115+
{
116+
/**
117+
* @var int
118+
* @Id
119+
* @Column(type="integer")
120+
* @GeneratedValue(strategy="AUTO")
121+
*/
122+
public $id;
123+
124+
/**
125+
* @var string
126+
* @Column(type="string", length=255)
127+
*/
128+
public $data;
129+
130+
/**
131+
* @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="phones")
132+
*
133+
* @var LazyEagerCollectionUser
134+
*/
135+
public $user;
136+
}
137+
138+
/** @Entity */
139+
class LazyEagerCollectionAddress
140+
{
141+
/**
142+
* @var int
143+
* @Id
144+
* @Column(type="integer")
145+
* @GeneratedValue(strategy="AUTO")
146+
*/
147+
public $id;
148+
149+
/**
150+
* @var string
151+
* @Column(type="string", length=255)
152+
*/
153+
public $data;
154+
155+
/**
156+
* @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="addresses")
157+
*
158+
* @var LazyEagerCollectionUser
159+
*/
160+
public $user;
161+
}

0 commit comments

Comments
 (0)