ORMInvalidArgumentException.php 9.3 KB

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