Completed
Pull Request — 2.7 (#8134)
by
unknown
06:51
created

GH8133ClassMany::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\OrmFunctionalTestCase;
9
10
/**
11
 * @group GH8133
12
 */
13
final class GH8133Test extends OrmFunctionalTestCase
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        $this->setUpEntitySchema([GH8133ClassOne::class, GH8133ClassMany::class]);
23
    }
24
25
    public function testReplacingElementsInOneToManyCollectionAfterPersistDoesNotCauseInsertionOfReplacedObject() : void
26
    {
27
        $gh8133ClassMany1 = new GH8133ClassMany(1);
28
        $gh8133ClassOne = new GH8133ClassOne([$gh8133ClassMany1]);
29
        $this->_em->persist($gh8133ClassOne);
30
31
        $gh8133ClassMany2 = new GH8133ClassMany(2);
32
        $gh8133ClassOne->replaceGh8133ClassManys([$gh8133ClassMany2]);
33
        $this->_em->flush();
34
        $this->_em->clear();
35
36
        $entity1 = $this->_em->getRepository(GH8133ClassMany::class)->find(1);
37
        self::assertNull($entity1);
38
        $entity2 = $this->_em->getRepository(GH8133ClassMany::class)->find(2);
39
        self::assertNotNull($entity2);
40
    }
41
}
42
43
/**
44
 * @Entity
45
 * @Table(name="gh8133_class_one")
46
 */
47
class GH8133ClassOne
48
{
49
    /**
50
     * @Id
51
     * @GeneratedValue
52
     * @Column(type="integer")
53
     * @var int
54
     */
55
    private $id;
56
57
    /**
58
     * @OneToMany(
59
     *     targetEntity="GH8133ClassMany",
60
     *     mappedBy="gh8133ClassOne",
61
     *     cascade={"persist"},
62
     *     orphanRemoval=true
63
     * )
64
     *
65
     * @var GH8133ClassMany[]|ArrayCollection
66
     */
67
    private $gh8133ClassManys;
68
69
    public function __construct(array $gh8133ClassManys)
70
    {
71
        $this->gh8133ClassManys = new ArrayCollection();
72
        $this->replaceGh8133ClassManys($gh8133ClassManys);
73
    }
74
75
    /**
76
     * @param GH8133ClassMany[] $gh8133ClassManys
77
     */
78
    public function replaceGh8133ClassManys(array $gh8133ClassManys): void
79
    {
80
        // Clear all the old objects replacing with the new ones
81
        $this->gh8133ClassManys->clear();
82
        foreach ($gh8133ClassManys as $gh8133ClassMany) {
83
            $this->gh8133ClassManys->add($gh8133ClassMany);
84
            // This should not be needed for persistence, only to keep it in sync objects.
85
            $gh8133ClassMany->gh8133ClassOne = $this;
86
        }
87
    }
88
}
89
90
/**
91
 * @Entity
92
 * @Table(name="gh8133_class_many")
93
 */
94
class GH8133ClassMany
95
{
96
    /**
97
     * @Id
98
     * @Column(type="integer")
99
     */
100
    private $id;
101
102
    /**
103
     * @ManyToOne(targetEntity="GH8133ClassOne", inversedBy="gh8133ClassManys")
104
     * @JoinColumn(name="gh_8133_class_one_id", nullable=false, onDelete="CASCADE")
105
     * @var GH8133ClassOne
106
     */
107
    public $gh8133ClassOne;
108
109
    public function __construct(int $id)
110
    {
111
        $this->id = $id;
112
    }
113
}
114