Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08e410e

Browse files
committed
Fix original data incomplete after flush
1 parent 710dde8 commit 08e410e

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

lib/Doctrine/ORM/UnitOfWork.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ public function computeChangeSet(ClassMetadata $class, $entity)
692692
if ($class->isCollectionValuedAssociation($name) && $value !== null) {
693693
if ($value instanceof PersistentCollection) {
694694
if ($value->getOwner() === $entity) {
695+
$actualData[$name] = $value;
695696
continue;
696697
}
697698

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Issue9300;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
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\ManyToMany;
14+
15+
/**
16+
* @Entity
17+
*/
18+
class Issue9300Child
19+
{
20+
/**
21+
* @var int
22+
* @Id
23+
* @Column(type="integer")
24+
* @GeneratedValue
25+
*/
26+
public $id;
27+
28+
/**
29+
* @var Collection|Issue9300Parent
30+
* @ManyToMany(targetEntity="Issue9300Parent")
31+
*/
32+
public $parents;
33+
34+
/**
35+
* @var string
36+
* @Column(type="string")
37+
*/
38+
public $name;
39+
40+
public function __construct()
41+
{
42+
$this->parents = new ArrayCollection();
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Issue9300;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
12+
/**
13+
* @Entity
14+
*/
15+
class Issue9300Parent
16+
{
17+
/**
18+
* @var int
19+
* @Id
20+
* @Column(type="integer")
21+
* @GeneratedValue
22+
*/
23+
public $id;
24+
25+
/**
26+
* @var string
27+
* @Column(type="string")
28+
*/
29+
public $name;
30+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Tests\Models\Issue9300\Issue9300Child;
9+
use Doctrine\Tests\Models\Issue9300\Issue9300Parent;
10+
use Doctrine\Tests\OrmFunctionalTestCase;
11+
12+
/**
13+
* @group 9300
14+
*/
15+
class Issue9300Test extends OrmFunctionalTestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
$this->useModelSet('issue9300');
20+
parent::setUp();
21+
}
22+
23+
/**
24+
* @group #9300
25+
*/
26+
public function testPersistedCollectionIsPresentInOriginalDataAfterFlush(): void
27+
{
28+
$parent = new Issue9300Parent();
29+
$child = new Issue9300Child();
30+
$child->parents->add($parent);
31+
32+
$parent->name = 'abc';
33+
$child->name = 'abc';
34+
35+
$this->_em->persist($parent);
36+
$this->_em->persist($child);
37+
$this->_em->flush();
38+
39+
$parent->name = 'abcd';
40+
$child->name = 'abcd';
41+
42+
$this->_em->flush();
43+
44+
self::assertArrayHasKey('parents', $this->_em->getUnitOfWork()->getOriginalEntityData($child));
45+
}
46+
47+
/**
48+
* @group #9300
49+
*/
50+
public function testPersistingCollectionAfterFlushWorksAsExpected(): void
51+
{
52+
$parentOne = new Issue9300Parent();
53+
$parentTwo = new Issue9300Parent();
54+
$childOne = new Issue9300Child();
55+
56+
$parentOne->name = 'abc';
57+
$parentTwo->name = 'abc';
58+
$childOne->name = 'abc';
59+
$childOne->parents = new ArrayCollection([$parentOne]);
60+
61+
$this->_em->persist($parentOne);
62+
$this->_em->persist($parentTwo);
63+
$this->_em->persist($childOne);
64+
$this->_em->flush();
65+
66+
// Recalculate change-set -> new original data
67+
$childOne->name = 'abcd';
68+
$this->_em->flush();
69+
70+
$childOne->parents = new ArrayCollection([$parentTwo]);
71+
72+
$this->_em->flush();
73+
$this->_em->clear();
74+
75+
$childOneFresh = $this->_em->find(Issue9300Child::class, $childOne->id);
76+
self::assertCount(1, $childOneFresh->parents);
77+
self::assertEquals($parentTwo->id, $childOneFresh->parents[0]->id);
78+
}
79+
}

tests/Doctrine/Tests/OrmFunctionalTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
338338
Models\Issue5989\Issue5989Employee::class,
339339
Models\Issue5989\Issue5989Manager::class,
340340
],
341+
'issue9300' => [
342+
Models\Issue9300\Issue9300Child::class,
343+
Models\Issue9300\Issue9300Parent::class,
344+
],
341345
];
342346

343347
/** @param class-string ...$models */

0 commit comments

Comments
 (0)