Skip to content

Commit 43ce0be

Browse files
committedNov 14, 2024
lazy and eager collection refresh
1 parent d9aa6ef commit 43ce0be

File tree

1 file changed

+160
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)
Please sign in to comment.