|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\AssociationMetadata; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use function array_map; |
|
11
|
|
|
use function count; |
|
12
|
|
|
use function get_class; |
|
13
|
|
|
use function gettype; |
|
14
|
|
|
use function implode; |
|
15
|
|
|
use function is_object; |
|
16
|
|
|
use function method_exists; |
|
17
|
|
|
use function reset; |
|
18
|
|
|
use function spl_object_id; |
|
19
|
|
|
use function sprintf; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork |
|
23
|
|
|
*/ |
|
24
|
|
|
class ORMInvalidArgumentException extends InvalidArgumentException |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param object $entity |
|
28
|
|
|
* |
|
29
|
|
|
* @return ORMInvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public static function scheduleInsertForManagedEntity($entity) |
|
32
|
|
|
{ |
|
33
|
1 |
|
return new self('A managed+dirty entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param object $entity |
|
38
|
|
|
* |
|
39
|
|
|
* @return ORMInvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public static function scheduleInsertForRemovedEntity($entity) |
|
42
|
|
|
{ |
|
43
|
1 |
|
return new self('Removed entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param object $entity |
|
48
|
|
|
* |
|
49
|
|
|
* @return ORMInvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public static function scheduleInsertTwice($entity) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return new self('Entity ' . self::objToStr($entity) . ' can not be scheduled for insertion twice.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $className |
|
58
|
|
|
* @param object $entity |
|
59
|
|
|
* |
|
60
|
|
|
* @return ORMInvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public static function entityWithoutIdentity($className, $entity) |
|
63
|
|
|
{ |
|
64
|
6 |
|
return new self( |
|
65
|
6 |
|
"The given entity of type '" . $className . "' (" . self::objToStr($entity) . ') has no identity/no ' . |
|
66
|
6 |
|
'id values set. It cannot be added to the identity map.' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param object $entity |
|
72
|
|
|
* |
|
73
|
|
|
* @return ORMInvalidArgumentException |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public static function readOnlyRequiresManagedEntity($entity) |
|
76
|
|
|
{ |
|
77
|
1 |
|
return new self('Only managed entities can be marked or checked as read only. But ' . self::objToStr($entity) . ' is not'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array of [$associationMetadata, $entity] pairs |
|
82
|
|
|
* |
|
83
|
|
|
* @return ORMInvalidArgumentException |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public static function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations) |
|
86
|
|
|
{ |
|
87
|
4 |
|
$errorMessages = array_map( |
|
88
|
|
|
static function (array $newEntityWithAssociation) : string { |
|
89
|
4 |
|
[$associationMetadata, $entity] = $newEntityWithAssociation; |
|
90
|
|
|
|
|
91
|
4 |
|
return self::newEntityFoundThroughRelationshipMessage($associationMetadata, $entity); |
|
92
|
4 |
|
}, |
|
93
|
4 |
|
$newEntitiesWithAssociations |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
4 |
|
if (count($errorMessages) === 1) { |
|
97
|
4 |
|
return new self(reset($errorMessages)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return new self( |
|
101
|
|
|
'Multiple non-persisted new entities were found through the given association graph:' |
|
102
|
|
|
. "\n\n * " |
|
103
|
|
|
. implode("\n * ", $errorMessages) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param object $entry |
|
109
|
|
|
* |
|
110
|
|
|
* @return ORMInvalidArgumentException |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function newEntityFoundThroughRelationship(AssociationMetadata $association, $entry) |
|
113
|
|
|
{ |
|
114
|
|
|
$message = "A new entity was found through the relationship '%s#%s' that was not configured to cascade " |
|
115
|
|
|
. 'persist operations for entity: %s. To solve this issue: Either explicitly call EntityManager#persist() ' |
|
116
|
|
|
. 'on this unknown entity or configure cascade persist this association in the mapping for example ' |
|
117
|
|
|
. '@ManyToOne(..,cascade={"persist"}).%s'; |
|
118
|
|
|
|
|
119
|
|
|
$messageAppend = method_exists($entry, '__toString') |
|
120
|
|
|
? '' |
|
121
|
|
|
: " If you cannot find out which entity causes the problem implement '%s#__toString()' to get a clue."; |
|
122
|
|
|
|
|
123
|
|
|
return new self(sprintf( |
|
124
|
|
|
$message, |
|
125
|
|
|
$association->getSourceEntity(), |
|
126
|
|
|
$association->getName(), |
|
127
|
|
|
self::objToStr($entry), |
|
128
|
|
|
sprintf($messageAppend, $association->getTargetEntity()) |
|
129
|
|
|
)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param object $entry |
|
134
|
|
|
* |
|
135
|
|
|
* @return ORMInvalidArgumentException |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function detachedEntityFoundThroughRelationship(AssociationMetadata $association, $entry) |
|
138
|
|
|
{ |
|
139
|
|
|
$messsage = "A detached entity of type %s (%s) was found through the relationship '%s#%s' during cascading a persist operation."; |
|
140
|
|
|
|
|
141
|
|
|
return new self(sprintf( |
|
142
|
|
|
$messsage, |
|
143
|
|
|
$association->getTargetEntity(), |
|
144
|
|
|
self::objToStr($entry), |
|
145
|
|
|
$association->getSourceEntity(), |
|
146
|
|
|
$association->getName() |
|
147
|
|
|
)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param object $entity |
|
152
|
|
|
* |
|
153
|
|
|
* @return ORMInvalidArgumentException |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public static function entityNotManaged($entity) |
|
156
|
|
|
{ |
|
157
|
1 |
|
return new self('Entity ' . self::objToStr($entity) . ' is not managed. An entity is managed if its fetched ' . |
|
158
|
1 |
|
'from the database or registered as new through EntityManager#persist'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param object $entity |
|
163
|
|
|
* @param string $operation |
|
164
|
|
|
* |
|
165
|
|
|
* @return ORMInvalidArgumentException |
|
166
|
|
|
*/ |
|
167
|
|
|
public static function entityHasNoIdentity($entity, $operation) |
|
168
|
|
|
{ |
|
169
|
|
|
return new self('Entity has no identity, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param object $entity |
|
174
|
|
|
* @param string $operation |
|
175
|
|
|
* |
|
176
|
|
|
* @return ORMInvalidArgumentException |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function entityIsRemoved($entity, $operation) |
|
179
|
|
|
{ |
|
180
|
|
|
return new self('Entity is removed, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param object $entity |
|
185
|
|
|
* @param string $operation |
|
186
|
|
|
* |
|
187
|
|
|
* @return ORMInvalidArgumentException |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function detachedEntityCannot($entity, $operation) |
|
190
|
|
|
{ |
|
191
|
|
|
return new self('Detached entity ' . self::objToStr($entity) . ' cannot be ' . $operation); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $context |
|
196
|
|
|
* @param mixed $given |
|
197
|
|
|
* @param int $parameterIndex |
|
198
|
|
|
* |
|
199
|
|
|
* @return ORMInvalidArgumentException |
|
200
|
|
|
*/ |
|
201
|
3 |
|
public static function invalidObject($context, $given, $parameterIndex = 1) |
|
202
|
|
|
{ |
|
203
|
3 |
|
return new self($context . ' expects parameter ' . $parameterIndex . |
|
204
|
3 |
|
' to be an entity object, ' . gettype($given) . ' given.'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return ORMInvalidArgumentException |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public static function invalidCompositeIdentifier() |
|
211
|
|
|
{ |
|
212
|
1 |
|
return new self('Binding an entity with a composite primary key to a query is not supported. ' . |
|
213
|
1 |
|
'You should split the parameter into the explicit fields and bind them separately.'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return ORMInvalidArgumentException |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public static function invalidIdentifierBindingEntity() |
|
220
|
|
|
{ |
|
221
|
1 |
|
return new self('Binding entities to query parameters only allowed for entities that have an identifier.'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param mixed $actualValue |
|
226
|
|
|
* |
|
227
|
|
|
* @return self |
|
228
|
|
|
*/ |
|
229
|
15 |
|
public static function invalidAssociation(ClassMetadata $targetClass, AssociationMetadata $association, $actualValue) |
|
230
|
|
|
{ |
|
231
|
15 |
|
$expectedType = $targetClass->getClassName(); |
|
232
|
|
|
|
|
233
|
15 |
|
return new self(sprintf( |
|
234
|
15 |
|
'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.', |
|
235
|
15 |
|
$expectedType, |
|
236
|
15 |
|
$association->getSourceEntity(), |
|
237
|
15 |
|
$association->getName(), |
|
238
|
15 |
|
is_object($actualValue) ? get_class($actualValue) : gettype($actualValue) |
|
239
|
|
|
)); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Helper method to show an object as string. |
|
244
|
|
|
* |
|
245
|
|
|
* @param object $obj |
|
246
|
|
|
*/ |
|
247
|
15 |
|
private static function objToStr($obj) : string |
|
248
|
|
|
{ |
|
249
|
15 |
|
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_id($obj); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param object $entity |
|
254
|
|
|
*/ |
|
255
|
4 |
|
private static function newEntityFoundThroughRelationshipMessage(AssociationMetadata $association, $entity) : string |
|
256
|
|
|
{ |
|
257
|
|
|
return 'A new entity was found through the relationship \'' |
|
258
|
4 |
|
. $association->getSourceEntity() . '#' . $association->getName() . '\' that was not' |
|
259
|
4 |
|
. ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.' |
|
260
|
4 |
|
. ' To solve this issue: Either explicitly call EntityManager#persist()' |
|
261
|
4 |
|
. ' on this unknown entity or configure cascade persist' |
|
262
|
4 |
|
. ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).' |
|
263
|
4 |
|
. (method_exists($entity, '__toString') |
|
264
|
1 |
|
? '' |
|
265
|
|
|
: ' If you cannot find out which entity causes the problem implement \'' |
|
266
|
4 |
|
. $association->getTargetEntity() . '#__toString()\' to get a clue.' |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|