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\Cache\Exception\NonCacheableEntityAssociation; |
9
|
|
|
use Doctrine\ORM\Mapping; |
10
|
|
|
use function count; |
11
|
|
|
use function in_array; |
12
|
|
|
|
13
|
|
|
abstract class ToOneAssociationMetadataBuilder extends AssociationMetadataBuilder |
14
|
|
|
{ |
15
|
|
|
/** @var JoinColumnMetadataBuilder */ |
16
|
|
|
protected $joinColumnMetadataBuilder; |
17
|
|
|
|
18
|
|
|
/** @var Annotation\Id|null */ |
19
|
|
|
protected $idAnnotation; |
20
|
|
|
|
21
|
|
|
/** @var Annotation\JoinColumn|null */ |
22
|
|
|
protected $joinColumnAnnotation; |
23
|
|
|
|
24
|
|
|
/** @var Annotation\JoinColumns|null */ |
25
|
|
|
protected $joinColumnsAnnotation; |
26
|
|
|
|
27
|
408 |
|
public function __construct( |
28
|
|
|
Mapping\ClassMetadataBuildingContext $metadataBuildingContext, |
29
|
|
|
?JoinColumnMetadataBuilder $joinColumnMetadataBuilder = null, |
30
|
|
|
?CacheMetadataBuilder $cacheMetadataBuilder = null |
31
|
|
|
) { |
32
|
408 |
|
parent::__construct($metadataBuildingContext, $cacheMetadataBuilder); |
33
|
|
|
|
34
|
408 |
|
$this->joinColumnMetadataBuilder = $joinColumnMetadataBuilder ?: new JoinColumnMetadataBuilder($metadataBuildingContext); |
35
|
408 |
|
} |
36
|
|
|
|
37
|
407 |
|
public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : AssociationMetadataBuilder |
38
|
|
|
{ |
39
|
407 |
|
parent::withComponentMetadata($componentMetadata); |
40
|
|
|
|
41
|
407 |
|
$this->joinColumnMetadataBuilder->withComponentMetadata($componentMetadata); |
42
|
|
|
|
43
|
407 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
407 |
|
public function withFieldName(string $fieldName) : AssociationMetadataBuilder |
47
|
|
|
{ |
48
|
407 |
|
parent::withFieldName($fieldName); |
49
|
|
|
|
50
|
407 |
|
$this->joinColumnMetadataBuilder->withFieldName($fieldName); |
51
|
|
|
|
52
|
407 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
407 |
|
public function withIdAnnotation(?Annotation\Id $idAnnotation) : ToOneAssociationMetadataBuilder |
56
|
|
|
{ |
57
|
407 |
|
$this->idAnnotation = $idAnnotation; |
58
|
|
|
|
59
|
407 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
386 |
|
public function withJoinColumnAnnotation(?Annotation\JoinColumn $joinColumnAnnotation) : ToOneAssociationMetadataBuilder |
63
|
|
|
{ |
64
|
386 |
|
$this->joinColumnAnnotation = $joinColumnAnnotation; |
65
|
|
|
|
66
|
386 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
386 |
|
public function withJoinColumnsAnnotation(?Annotation\JoinColumns $joinColumnsAnnotation) : ToOneAssociationMetadataBuilder |
70
|
|
|
{ |
71
|
386 |
|
$this->joinColumnsAnnotation = $joinColumnsAnnotation; |
72
|
|
|
|
73
|
386 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
237 |
|
protected function buildPrimaryKey(Mapping\AssociationMetadata $associationMetadata) : void |
77
|
|
|
{ |
78
|
237 |
|
if ($this->idAnnotation !== null) { |
79
|
43 |
|
if ($associationMetadata->isOrphanRemoval()) { |
80
|
|
|
throw Mapping\MappingException::illegalOrphanRemovalOnIdentifierAssociation( |
81
|
|
|
$this->componentMetadata->getClassName(), |
82
|
|
|
$this->fieldName |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
43 |
|
if (! $associationMetadata->isOwningSide()) { |
87
|
|
|
throw Mapping\MappingException::illegalInverseIdentifierAssociation( |
88
|
|
|
$this->componentMetadata->getClassName(), |
89
|
|
|
$this->fieldName |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
43 |
|
if ($this->componentMetadata->getCache() !== null && $associationMetadata->getCache() === null) { |
94
|
2 |
|
throw NonCacheableEntityAssociation::fromEntityAndField( |
95
|
2 |
|
$this->componentMetadata->getClassName(), |
96
|
2 |
|
$this->fieldName |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// @todo guilhermeblanco The below block of code modifies component metadata properties, and it should be moved |
101
|
|
|
// to the component metadata builder instead of here. |
102
|
|
|
|
103
|
41 |
|
if (! in_array($this->fieldName, $this->componentMetadata->identifier, true)) { |
104
|
41 |
|
$this->componentMetadata->identifier[] = $this->fieldName; |
105
|
|
|
} |
106
|
|
|
|
107
|
41 |
|
$associationMetadata->setPrimaryKey(true); |
108
|
|
|
} |
109
|
235 |
|
} |
110
|
|
|
|
111
|
232 |
|
protected function buildJoinColumns(Mapping\ToOneAssociationMetadata $associationMetadata) : void |
112
|
|
|
{ |
113
|
232 |
|
switch (true) { |
114
|
232 |
|
case $this->joinColumnsAnnotation !== null: |
115
|
19 |
|
foreach ($this->joinColumnsAnnotation->value as $joinColumnAnnotation) { |
116
|
19 |
|
$this->joinColumnMetadataBuilder->withJoinColumnAnnotation($joinColumnAnnotation); |
117
|
|
|
|
118
|
19 |
|
$joinColumnMetadata = $this->joinColumnMetadataBuilder->build(); |
119
|
19 |
|
$dataTypeResolver = $this->createLazyDataTypeResolver( |
120
|
19 |
|
$this->metadataBuildingContext, |
121
|
|
|
$associationMetadata, |
122
|
|
|
$joinColumnMetadata, |
123
|
19 |
|
$associationMetadata->getTargetEntity() |
124
|
|
|
); |
125
|
|
|
|
126
|
19 |
|
$joinColumnMetadata->setType($dataTypeResolver); |
127
|
|
|
|
128
|
19 |
|
$associationMetadata->addJoinColumn($joinColumnMetadata); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Prevent currently unsupported scenario: association with multiple columns and being marked as primary |
132
|
19 |
|
if ($associationMetadata->isPrimaryKey() && count($associationMetadata->getJoinColumns()) > 1) { |
133
|
|
|
throw Mapping\MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId( |
134
|
|
|
$this->componentMetadata->getClassName(), |
135
|
|
|
$associationMetadata->getTargetEntity(), |
136
|
|
|
$this->fieldName |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
19 |
|
break; |
141
|
|
|
|
142
|
225 |
|
case $this->joinColumnAnnotation !== null: |
143
|
158 |
|
$this->joinColumnMetadataBuilder->withJoinColumnAnnotation($this->joinColumnAnnotation); |
144
|
|
|
|
145
|
158 |
|
$joinColumnMetadata = $this->joinColumnMetadataBuilder->build(); |
146
|
158 |
|
$dataTypeResolver = $this->createLazyDataTypeResolver( |
147
|
158 |
|
$this->metadataBuildingContext, |
148
|
|
|
$associationMetadata, |
149
|
|
|
$joinColumnMetadata, |
150
|
158 |
|
$associationMetadata->getTargetEntity() |
151
|
|
|
); |
152
|
|
|
|
153
|
158 |
|
$joinColumnMetadata->setType($dataTypeResolver); |
154
|
|
|
|
155
|
158 |
|
$associationMetadata->addJoinColumn($joinColumnMetadata); |
156
|
158 |
|
break; |
157
|
|
|
|
158
|
|
|
default: |
159
|
76 |
|
$joinColumnMetadata = $this->joinColumnMetadataBuilder->build(); |
160
|
76 |
|
$dataTypeResolver = $this->createLazyDataTypeResolver( |
161
|
76 |
|
$this->metadataBuildingContext, |
162
|
|
|
$associationMetadata, |
163
|
|
|
$joinColumnMetadata, |
164
|
76 |
|
$associationMetadata->getTargetEntity() |
165
|
|
|
); |
166
|
|
|
|
167
|
76 |
|
$joinColumnMetadata->setType($dataTypeResolver); |
168
|
|
|
|
169
|
76 |
|
$associationMetadata->addJoinColumn($joinColumnMetadata); |
170
|
76 |
|
break; |
171
|
|
|
} |
172
|
232 |
|
} |
173
|
|
|
} |
174
|
|
|
|