ORMInvalidArgumentException.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /**
  21. * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
  22. *
  23. * @author Benjamin Eberlei <kontakt@beberlei.de>
  24. */
  25. class ORMInvalidArgumentException extends \InvalidArgumentException
  26. {
  27. /**
  28. * @param object $entity
  29. *
  30. * @return ORMInvalidArgumentException
  31. */
  32. static public function scheduleInsertForManagedEntity($entity)
  33. {
  34. return new self("A managed+dirty entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
  35. }
  36. /**
  37. * @param object $entity
  38. *
  39. * @return ORMInvalidArgumentException
  40. */
  41. static public function scheduleInsertForRemovedEntity($entity)
  42. {
  43. return new self("Removed entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
  44. }
  45. /**
  46. * @param object $entity
  47. *
  48. * @return ORMInvalidArgumentException
  49. */
  50. static public function scheduleInsertTwice($entity)
  51. {
  52. return new self("Entity " . self::objToStr($entity) . " can not be scheduled for insertion twice.");
  53. }
  54. /**
  55. * @param string $className
  56. * @param object $entity
  57. *
  58. * @return ORMInvalidArgumentException
  59. */
  60. static public function entityWithoutIdentity($className, $entity)
  61. {
  62. return new self(
  63. "The given entity of type '" . $className . "' (".self::objToStr($entity).") has no identity/no " .
  64. "id values set. It cannot be added to the identity map."
  65. );
  66. }
  67. /**
  68. * @param object $entity
  69. *
  70. * @return ORMInvalidArgumentException
  71. */
  72. static public function readOnlyRequiresManagedEntity($entity)
  73. {
  74. return new self("Only managed entities can be marked or checked as read only. But " . self::objToStr($entity) . " is not");
  75. }
  76. /**
  77. * @param array $assoc
  78. * @param object $entry
  79. *
  80. * @return ORMInvalidArgumentException
  81. */
  82. static public function newEntityFoundThroughRelationship(array $assoc, $entry)
  83. {
  84. return new self("A new entity was found through the relationship '"
  85. . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not"
  86. . " configured to cascade persist operations for entity: " . self::objToStr($entry) . "."
  87. . " To solve this issue: Either explicitly call EntityManager#persist()"
  88. . " on this unknown entity or configure cascade persist "
  89. . " this association in the mapping for example @ManyToOne(..,cascade={\"persist\"})."
  90. . (method_exists($entry, '__toString') ?
  91. "":
  92. " If you cannot find out which entity causes the problem"
  93. ." implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue."));
  94. }
  95. /**
  96. * @param array $assoc
  97. * @param object $entry
  98. *
  99. * @return ORMInvalidArgumentException
  100. */
  101. static public function detachedEntityFoundThroughRelationship(array $assoc, $entry)
  102. {
  103. return new self("A detached entity of type " . $assoc['targetEntity'] . " (" . self::objToStr($entry) . ") "
  104. . " was found through the relationship '" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' "
  105. . "during cascading a persist operation.");
  106. }
  107. /**
  108. * @param object $entity
  109. *
  110. * @return ORMInvalidArgumentException
  111. */
  112. static public function entityNotManaged($entity)
  113. {
  114. return new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " .
  115. "from the database or registered as new through EntityManager#persist");
  116. }
  117. /**
  118. * @param object $entity
  119. * @param string $operation
  120. *
  121. * @return ORMInvalidArgumentException
  122. */
  123. static public function entityHasNoIdentity($entity, $operation)
  124. {
  125. return new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  126. }
  127. /**
  128. * @param object $entity
  129. * @param string $operation
  130. *
  131. * @return ORMInvalidArgumentException
  132. */
  133. static public function entityIsRemoved($entity, $operation)
  134. {
  135. return new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  136. }
  137. /**
  138. * @param object $entity
  139. * @param string $operation
  140. *
  141. * @return ORMInvalidArgumentException
  142. */
  143. static public function detachedEntityCannot($entity, $operation)
  144. {
  145. return new self("A detached entity was found during " . $operation . " " . self::objToStr($entity));
  146. }
  147. /**
  148. * @param string $context
  149. * @param mixed $given
  150. * @param int $parameterIndex
  151. *
  152. * @return ORMInvalidArgumentException
  153. */
  154. public static function invalidObject($context, $given, $parameterIndex = 1)
  155. {
  156. return new self($context . ' expects parameter ' . $parameterIndex .
  157. ' to be an entity object, '. gettype($given) . ' given.');
  158. }
  159. /**
  160. * @return ORMInvalidArgumentException
  161. */
  162. public static function invalidCompositeIdentifier()
  163. {
  164. return new self("Binding an entity with a composite primary key to a query is not supported. " .
  165. "You should split the parameter into the explicit fields and bind them separately.");
  166. }
  167. /**
  168. * @return ORMInvalidArgumentException
  169. */
  170. public static function invalidIdentifierBindingEntity()
  171. {
  172. return new self("Binding entities to query parameters only allowed for entities that have an identifier.");
  173. }
  174. /**
  175. * Helper method to show an object as string.
  176. *
  177. * @param object $obj
  178. *
  179. * @return string
  180. */
  181. private static function objToStr($obj)
  182. {
  183. return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj);
  184. }
  185. }