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

GH8133ClassOne   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A replaceGh8133ClassManys() 0 8 2
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
        $entityNotFound = $this->_em->getRepository(GH8133ClassMany::class)->find(1);
37
        self::assertNull($entityNotFound);
38
        $entityFound = $this->_em->getRepository(GH8133ClassMany::class)->find(2);
39
        self::assertNotNull($entityFound);
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
    public $id;
56
57
    /**
58
     * @OneToMany(
59
     *     targetEntity="GH8133ClassMany",
60
     *     mappedBy="gh8133ClassOne",
61
     *     cascade={"persist"}
62
     * )
63
     *
64
     * @var GH8133ClassMany[]|ArrayCollection
65
     */
66
    private $gh8133ClassManys;
67
68
    public function __construct(array $gh8133ClassManys)
69
    {
70
        $this->gh8133ClassManys = new ArrayCollection();
71
        $this->replaceGh8133ClassManys($gh8133ClassManys);
72
    }
73
74
    /**
75
     * @param GH8133ClassMany[] $gh8133ClassManys
76
     */
77
    public function replaceGh8133ClassManys(array $gh8133ClassManys): void
78
    {
79
        // Clear all the old objects replacing with the new ones
80
        $this->gh8133ClassManys->clear();
81
        foreach ($gh8133ClassManys as $gh8133ClassMany) {
82
            $this->gh8133ClassManys->add($gh8133ClassMany);
83
            // This should not be needed for persistence, only to keep it in sync objects.
84
            $gh8133ClassMany->gh8133ClassOne = $this;
85
        }
86
    }
87
}
88
89
/**
90
 * @Entity
91
 * @Table(name="gh8133_class_many")
92
 */
93
class GH8133ClassMany
94
{
95
    /**
96
     * @Id
97
     * @Column(type="integer")
98
     */
99
    public $id;
100
101
    /**
102
     * @ManyToOne(targetEntity="GH8133ClassOne", inversedBy="gh8133ClassManys")
103
     * @JoinColumn(name="gh_8133_class_one_id", nullable=false, onDelete="CASCADE")
104
     * @var GH8133ClassOne
105
     */
106
    public $gh8133ClassOne;
107
108
    public function __construct(int $id)
109
    {
110
        $this->id = $id;
111
    }
112
}
113