|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Annotation; |
|
8
|
|
|
use Doctrine\ORM\Mapping; |
|
9
|
|
|
use function array_merge; |
|
10
|
|
|
use function array_unique; |
|
11
|
|
|
use function assert; |
|
12
|
|
|
|
|
13
|
|
|
class ManyToManyAssociationMetadataBuilder extends ToManyAssociationMetadataBuilder |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var JoinTableMetadataBuilder */ |
|
16
|
|
|
private $joinTableMetadataBuilder; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Annotation\ManyToMany */ |
|
19
|
|
|
private $manyToManyAnnotation; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Annotation\JoinTable|null */ |
|
22
|
|
|
private $joinTableAnnotation; |
|
23
|
|
|
|
|
24
|
103 |
|
public function __construct( |
|
25
|
|
|
Mapping\ClassMetadataBuildingContext $metadataBuildingContext, |
|
26
|
|
|
?JoinTableMetadataBuilder $joinTableMetadataBuilder = null, |
|
27
|
|
|
?CacheMetadataBuilder $cacheMetadataBuilder = null |
|
28
|
|
|
) { |
|
29
|
103 |
|
parent::__construct($metadataBuildingContext, $cacheMetadataBuilder); |
|
30
|
|
|
|
|
31
|
103 |
|
$this->joinTableMetadataBuilder = $joinTableMetadataBuilder ?: new JoinTableMetadataBuilder($metadataBuildingContext); |
|
32
|
103 |
|
} |
|
33
|
|
|
|
|
34
|
103 |
|
public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : AssociationMetadataBuilder |
|
35
|
|
|
{ |
|
36
|
103 |
|
parent::withComponentMetadata($componentMetadata); |
|
37
|
|
|
|
|
38
|
103 |
|
$this->joinTableMetadataBuilder->withComponentMetadata($componentMetadata); |
|
39
|
|
|
|
|
40
|
103 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
103 |
|
public function withFieldName(string $fieldName) : AssociationMetadataBuilder |
|
44
|
|
|
{ |
|
45
|
103 |
|
parent::withFieldName($fieldName); |
|
46
|
|
|
|
|
47
|
103 |
|
$this->joinTableMetadataBuilder->withFieldName($fieldName); |
|
48
|
|
|
|
|
49
|
103 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
103 |
|
public function withManyToManyAnnotation(Annotation\ManyToMany $manyToManyAnnotation) : ManyToManyAssociationMetadataBuilder |
|
53
|
|
|
{ |
|
54
|
103 |
|
$this->manyToManyAnnotation = $manyToManyAnnotation; |
|
55
|
|
|
|
|
56
|
103 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
103 |
|
public function withJoinTableAnnotation(?Annotation\JoinTable $joinTableAnnotation) : ManyToManyAssociationMetadataBuilder |
|
60
|
|
|
{ |
|
61
|
103 |
|
$this->joinTableAnnotation = $joinTableAnnotation; |
|
62
|
|
|
|
|
63
|
103 |
|
if ($joinTableAnnotation !== null) { |
|
64
|
79 |
|
$this->joinTableMetadataBuilder->withJoinTableAnnotation($joinTableAnnotation); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
103 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @internal Association metadata order of definition settings is important. |
|
72
|
|
|
*/ |
|
73
|
103 |
|
public function build() : Mapping\ManyToManyAssociationMetadata |
|
74
|
|
|
{ |
|
75
|
|
|
// Validate required fields |
|
76
|
103 |
|
assert($this->componentMetadata !== null); |
|
77
|
103 |
|
assert($this->manyToManyAnnotation !== null); |
|
78
|
103 |
|
assert($this->fieldName !== null); |
|
79
|
|
|
|
|
80
|
103 |
|
$componentClassName = $this->componentMetadata->getClassName(); |
|
81
|
103 |
|
$associationMetadata = new Mapping\ManyToManyAssociationMetadata($this->fieldName); |
|
82
|
|
|
|
|
83
|
103 |
|
$associationMetadata->setSourceEntity($componentClassName); |
|
84
|
103 |
|
$associationMetadata->setTargetEntity($this->getTargetEntity($this->manyToManyAnnotation->targetEntity)); |
|
85
|
103 |
|
$associationMetadata->setCascade($this->getCascade($this->manyToManyAnnotation->cascade)); |
|
86
|
103 |
|
$associationMetadata->setFetchMode($this->getFetchMode($this->manyToManyAnnotation->fetch)); |
|
87
|
103 |
|
$associationMetadata->setOwningSide(true); |
|
88
|
|
|
|
|
89
|
103 |
|
if (! empty($this->manyToManyAnnotation->mappedBy)) { |
|
90
|
41 |
|
$associationMetadata->setMappedBy($this->manyToManyAnnotation->mappedBy); |
|
91
|
41 |
|
$associationMetadata->setOwningSide(false); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
103 |
|
if (! empty($this->manyToManyAnnotation->inversedBy)) { |
|
95
|
51 |
|
$associationMetadata->setInversedBy($this->manyToManyAnnotation->inversedBy); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
103 |
|
if (! empty($this->manyToManyAnnotation->indexBy)) { |
|
99
|
3 |
|
$associationMetadata->setIndexedBy($this->manyToManyAnnotation->indexBy); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
103 |
|
if ($this->manyToManyAnnotation->orphanRemoval) { |
|
103
|
1 |
|
$associationMetadata->setOrphanRemoval($this->manyToManyAnnotation->orphanRemoval); |
|
104
|
|
|
|
|
105
|
|
|
// Orphan removal also implies a cascade remove |
|
106
|
1 |
|
$associationMetadata->setCascade(array_unique(array_merge($associationMetadata->getCascade(), ['remove']))); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
103 |
|
if ($this->orderByAnnotation !== null) { |
|
110
|
4 |
|
$associationMetadata->setOrderBy($this->orderByAnnotation->value); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
103 |
|
$this->buildCache($associationMetadata); |
|
114
|
|
|
|
|
115
|
|
|
// Check for owning side to consider join column |
|
116
|
103 |
|
if (! $associationMetadata->isOwningSide()) { |
|
117
|
41 |
|
return $associationMetadata; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
89 |
|
$this->buildJoinTable($associationMetadata); |
|
121
|
|
|
|
|
122
|
89 |
|
return $associationMetadata; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
89 |
|
protected function buildJoinTable(Mapping\ManyToManyAssociationMetadata $associationMetadata) : void |
|
126
|
|
|
{ |
|
127
|
89 |
|
$this->joinTableMetadataBuilder->withTargetEntity($associationMetadata->getTargetEntity()); |
|
128
|
|
|
|
|
129
|
89 |
|
$associationMetadata->setJoinTable($this->joinTableMetadataBuilder->build()); |
|
130
|
|
|
} |
|
131
|
|
|
} |