Completed
Pull Request — 2.7 (#8134)
by
unknown
07:01
created

GH8134ClassMany::__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 GH8134
12
 */
13
final class GH8134Test extends OrmFunctionalTestCase
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        $this->setUpEntitySchema([GH8134ClassOne::class, GH8134ClassMany::class]);
23
    }
24
25
    public function testReplacingElementsInOneToManyCollectionAfterPersistDoesNotCauseInsertionOfReplacedObject() : void
26
    {
27
        $gh8134ClassMany1 = new GH8134ClassMany(1);
28
        $gh8134ClassOne = new GH8134ClassOne([$gh8134ClassMany1]);
29
        $this->_em->persist($gh8134ClassOne);
30
31
        $gh8134ClassMany2 = new GH8134ClassMany(2);
32
        $gh8134ClassOne->replaceGh8134ClassManys([$gh8134ClassMany2]);
33
        $this->_em->flush();
34
        $this->_em->clear();
35
36
        $entityNotFound = $this->_em->getRepository(GH8134ClassMany::class)->find(1);
37
        self::assertNull($entityNotFound);
38
        $entityFound = $this->_em->getRepository(GH8134ClassMany::class)->find(2);
39
        self::assertNotNull($entityFound);
40
    }
41
}
42
43
/**
44
 * @Entity
45
 * @Table(name="gh8134_class_one")
46
 */
47
class GH8134ClassOne
48
{
49
    /**
50
     * @Id
51
     * @GeneratedValue
52
     * @Column(type="integer")
53
     * @var int
54
     */
55
    public $id;
56
57
    /**
58
     * @OneToMany(
59
     *     targetEntity="GH8134ClassMany",
60
     *     mappedBy="gh8134ClassOne",
61
     *     cascade={"persist"}
62
     * )
63
     *
64
     * @var GH8134ClassMany[]|ArrayCollection
65
     */
66
    private $gh8134ClassManys;
67
68
    public function __construct(array $gh8134ClassManys)
69
    {
70
        $this->gh8134ClassManys = new ArrayCollection();
71
        $this->replaceGh8134ClassManys($gh8134ClassManys);
72
    }
73
74
    /**
75
     * @param GH8134ClassMany[] $gh8134ClassManys
76
     */
77
    public function replaceGh8134ClassManys(array $gh8134ClassManys): void
78
    {
79
        // Clear all the old objects replacing with the new ones
80
        $this->gh8134ClassManys->clear();
81
        foreach ($gh8134ClassManys as $gh8134ClassMany) {
82
            $this->gh8134ClassManys->add($gh8134ClassMany);
83
            // This should not be needed for persistence, only to keep it in sync objects.
84
            $gh8134ClassMany->gh8134ClassOne = $this;
85
        }
86
    }
87
}
88
89
/**
90
 * @Entity
91
 * @Table(name="gh8134_class_many")
92
 */
93
class GH8134ClassMany
94
{
95
    /**
96
     * @Id
97
     * @Column(type="integer")
98
     */
99
    public $id;
100
101
    /**
102
     * @ManyToOne(targetEntity="GH8134ClassOne", inversedBy="gh8134ClassManys")
103
     * @JoinColumn(name="gh_8134_class_one_id", nullable=false, onDelete="CASCADE")
104
     * @var GH8134ClassOne
105
     */
106
    public $gh8134ClassOne;
107
108
    public function __construct(int $id)
109
    {
110
        $this->id = $id;
111
    }
112
}
113