1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM; |
21
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork |
25
|
|
|
* |
26
|
|
|
* @author Benjamin Eberlei <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ORMInvalidArgumentException extends \InvalidArgumentException |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param object $entity |
32
|
|
|
* |
33
|
|
|
* @return ORMInvalidArgumentException |
34
|
|
|
*/ |
35
|
1 |
|
static public function scheduleInsertForManagedEntity($entity) |
|
|
|
|
36
|
|
|
{ |
37
|
1 |
|
return new self("A managed+dirty entity " . self::objToStr($entity) . " can not be scheduled for insertion."); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object $entity |
42
|
|
|
* |
43
|
|
|
* @return ORMInvalidArgumentException |
44
|
|
|
*/ |
45
|
1 |
|
static public function scheduleInsertForRemovedEntity($entity) |
|
|
|
|
46
|
|
|
{ |
47
|
1 |
|
return new self("Removed entity " . self::objToStr($entity) . " can not be scheduled for insertion."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param object $entity |
52
|
|
|
* |
53
|
|
|
* @return ORMInvalidArgumentException |
54
|
|
|
*/ |
55
|
1 |
|
static public function scheduleInsertTwice($entity) |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
return new self("Entity " . self::objToStr($entity) . " can not be scheduled for insertion twice."); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $className |
62
|
|
|
* @param object $entity |
63
|
|
|
* |
64
|
|
|
* @return ORMInvalidArgumentException |
65
|
|
|
*/ |
66
|
6 |
|
static public function entityWithoutIdentity($className, $entity) |
|
|
|
|
67
|
|
|
{ |
68
|
6 |
|
return new self( |
69
|
6 |
|
"The given entity of type '" . $className . "' (".self::objToStr($entity).") has no identity/no " . |
70
|
6 |
|
"id values set. It cannot be added to the identity map." |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param object $entity |
76
|
|
|
* |
77
|
|
|
* @return ORMInvalidArgumentException |
78
|
|
|
*/ |
79
|
1 |
|
static public function readOnlyRequiresManagedEntity($entity) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
return new self("Only managed entities can be marked or checked as read only. But " . self::objToStr($entity) . " is not"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array |
86
|
|
|
* of [array $associationMapping, object $entity] pairs |
87
|
|
|
* |
88
|
|
|
* @return ORMInvalidArgumentException |
89
|
|
|
*/ |
90
|
8 |
|
static public function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations) |
|
|
|
|
91
|
|
|
{ |
92
|
8 |
|
$errorMessages = array_map( |
93
|
8 |
|
function (array $newEntityWithAssociation) : string { |
94
|
8 |
|
[$associationMapping, $entity] = $newEntityWithAssociation; |
|
|
|
|
95
|
|
|
|
96
|
8 |
|
return self::newEntityFoundThroughRelationshipMessage($associationMapping, $entity); |
|
|
|
|
97
|
8 |
|
}, |
98
|
8 |
|
$newEntitiesWithAssociations |
99
|
|
|
); |
100
|
|
|
|
101
|
8 |
|
if (1 === count($errorMessages)) { |
102
|
7 |
|
return new self(reset($errorMessages)); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return new self( |
106
|
|
|
'Multiple non-persisted new entities were found through the given association graph:' |
107
|
|
|
. "\n\n * " |
108
|
1 |
|
. implode("\n * ", $errorMessages) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $associationMapping |
114
|
|
|
* @param object $entry |
115
|
|
|
* |
116
|
|
|
* @return ORMInvalidArgumentException |
117
|
|
|
*/ |
118
|
|
|
static public function newEntityFoundThroughRelationship(array $associationMapping, $entry) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return new self(self::newEntityFoundThroughRelationshipMessage($associationMapping, $entry)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $assoc |
125
|
|
|
* @param object $entry |
126
|
|
|
* |
127
|
|
|
* @return ORMInvalidArgumentException |
128
|
|
|
*/ |
129
|
|
|
static public function detachedEntityFoundThroughRelationship(array $assoc, $entry) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
return new self("A detached entity of type " . $assoc['targetEntity'] . " (" . self::objToStr($entry) . ") " |
132
|
|
|
. " was found through the relationship '" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' " |
133
|
|
|
. "during cascading a persist operation."); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param object $entity |
138
|
|
|
* |
139
|
|
|
* @return ORMInvalidArgumentException |
140
|
|
|
*/ |
141
|
1 |
|
static public function entityNotManaged($entity) |
|
|
|
|
142
|
|
|
{ |
143
|
1 |
|
return new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " . |
144
|
1 |
|
"from the database or registered as new through EntityManager#persist"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param object $entity |
149
|
|
|
* @param string $operation |
150
|
|
|
* |
151
|
|
|
* @return ORMInvalidArgumentException |
152
|
|
|
*/ |
153
|
|
|
static public function entityHasNoIdentity($entity, $operation) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param object $entity |
160
|
|
|
* @param string $operation |
161
|
|
|
* |
162
|
|
|
* @return ORMInvalidArgumentException |
163
|
|
|
*/ |
164
|
|
|
static public function entityIsRemoved($entity, $operation) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
return new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param object $entity |
171
|
|
|
* @param string $operation |
172
|
|
|
* |
173
|
|
|
* @return ORMInvalidArgumentException |
174
|
|
|
*/ |
175
|
|
|
static public function detachedEntityCannot($entity, $operation) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
return new self("Detached entity " . self::objToStr($entity) . " cannot be " . $operation); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $context |
182
|
|
|
* @param mixed $given |
183
|
|
|
* @param int $parameterIndex |
184
|
|
|
* |
185
|
|
|
* @return ORMInvalidArgumentException |
186
|
|
|
*/ |
187
|
5 |
|
public static function invalidObject($context, $given, $parameterIndex = 1) |
188
|
|
|
{ |
189
|
5 |
|
return new self($context . ' expects parameter ' . $parameterIndex . |
190
|
5 |
|
' to be an entity object, '. gettype($given) . ' given.'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return ORMInvalidArgumentException |
195
|
|
|
*/ |
196
|
|
|
public static function invalidCompositeIdentifier() |
197
|
|
|
{ |
198
|
|
|
return new self("Binding an entity with a composite primary key to a query is not supported. " . |
199
|
|
|
"You should split the parameter into the explicit fields and bind them separately."); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return ORMInvalidArgumentException |
204
|
|
|
*/ |
205
|
1 |
|
public static function invalidIdentifierBindingEntity() |
206
|
|
|
{ |
207
|
1 |
|
return new self("Binding entities to query parameters only allowed for entities that have an identifier."); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param ClassMetadata $targetClass |
212
|
|
|
* @param array $assoc |
213
|
|
|
* @param mixed $actualValue |
214
|
|
|
* |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
13 |
|
public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue) |
218
|
|
|
{ |
219
|
13 |
|
$expectedType = 'Doctrine\Common\Collections\Collection|array'; |
220
|
|
|
|
221
|
13 |
|
if (($assoc['type'] & ClassMetadata::TO_ONE) > 0) { |
222
|
13 |
|
$expectedType = $targetClass->getName(); |
223
|
|
|
} |
224
|
|
|
|
225
|
13 |
|
return new self(sprintf( |
226
|
13 |
|
'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.', |
227
|
13 |
|
$expectedType, |
228
|
13 |
|
$assoc['sourceEntity'], |
229
|
13 |
|
$assoc['fieldName'], |
230
|
13 |
|
is_object($actualValue) ? get_class($actualValue) : gettype($actualValue) |
231
|
|
|
)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Used when a given entityName hasn't the good type |
236
|
|
|
* |
237
|
|
|
* @param mixed $entityName The given entity (which shouldn't be a string) |
238
|
|
|
* |
239
|
|
|
* @return self |
240
|
|
|
*/ |
241
|
6 |
|
public static function invalidEntityName($entityName) |
242
|
|
|
{ |
243
|
6 |
|
return new self(sprintf('Entity name must be a string, %s given', gettype($entityName))); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Helper method to show an object as string. |
248
|
|
|
* |
249
|
|
|
* @param object $obj |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
19 |
|
private static function objToStr($obj) : string |
254
|
|
|
{ |
255
|
19 |
|
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param array $associationMapping |
260
|
|
|
* @param object $entity |
261
|
|
|
*/ |
262
|
8 |
|
private static function newEntityFoundThroughRelationshipMessage(array $associationMapping, $entity) : string |
263
|
|
|
{ |
264
|
|
|
return 'A new entity was found through the relationship \'' |
265
|
8 |
|
. $associationMapping['sourceEntity'] . '#' . $associationMapping['fieldName'] . '\' that was not' |
266
|
8 |
|
. ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.' |
267
|
8 |
|
. ' To solve this issue: Either explicitly call EntityManager#persist()' |
268
|
8 |
|
. ' on this unknown entity or configure cascade persist' |
269
|
8 |
|
. ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).' |
270
|
8 |
|
. (method_exists($entity, '__toString') |
271
|
2 |
|
? '' |
272
|
|
|
: ' If you cannot find out which entity causes the problem implement \'' |
273
|
8 |
|
. $associationMapping['targetEntity'] . '#__toString()\' to get a clue.' |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|