ORMException.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Exception;
  21. /**
  22. * Base exception class for all ORM exceptions.
  23. *
  24. * @author Roman Borschel <roman@code-factory.org>
  25. * @since 2.0
  26. */
  27. class ORMException extends Exception
  28. {
  29. /**
  30. * @return ORMException
  31. */
  32. public static function missingMappingDriverImpl()
  33. {
  34. return new self("It's a requirement to specify a Metadata Driver and pass it ".
  35. "to Doctrine\\ORM\\Configuration::setMetadataDriverImpl().");
  36. }
  37. /**
  38. * @param string $queryName
  39. *
  40. * @return ORMException
  41. */
  42. public static function namedQueryNotFound($queryName)
  43. {
  44. return new self('Could not find a named query by the name "' . $queryName . '"');
  45. }
  46. /**
  47. * @param string $nativeQueryName
  48. *
  49. * @return ORMException
  50. */
  51. public static function namedNativeQueryNotFound($nativeQueryName)
  52. {
  53. return new self('Could not find a named native query by the name "' . $nativeQueryName . '"');
  54. }
  55. /**
  56. * @param object $entity
  57. * @param object $relatedEntity
  58. *
  59. * @return ORMException
  60. */
  61. public static function entityMissingForeignAssignedId($entity, $relatedEntity)
  62. {
  63. return new self(
  64. "Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntity) . ", " .
  65. "however this entity has no identity itself. You have to call EntityManager#persist() on the related entity " .
  66. "and make sure that an identifier was generated before trying to persist '" . get_class($entity) . "'. In case " .
  67. "of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call " .
  68. "EntityManager#flush() between both persist operations."
  69. );
  70. }
  71. /**
  72. * @param object $entity
  73. * @param string $field
  74. *
  75. * @return ORMException
  76. */
  77. public static function entityMissingAssignedIdForField($entity, $field)
  78. {
  79. return new self("Entity of type " . get_class($entity) . " is missing an assigned ID for field '" . $field . "'. " .
  80. "The identifier generation strategy for this entity requires the ID field to be populated before ".
  81. "EntityManager#persist() is called. If you want automatically generated identifiers instead " .
  82. "you need to adjust the metadata mapping accordingly."
  83. );
  84. }
  85. /**
  86. * @param string $field
  87. *
  88. * @return ORMException
  89. */
  90. public static function unrecognizedField($field)
  91. {
  92. return new self("Unrecognized field: $field");
  93. }
  94. /**
  95. * @param string $className
  96. * @param string $field
  97. *
  98. * @return ORMException
  99. */
  100. public static function invalidOrientation($className, $field)
  101. {
  102. return new self("Invalid order by orientation specified for " . $className . "#" . $field);
  103. }
  104. /**
  105. * @param string $mode
  106. *
  107. * @return ORMException
  108. */
  109. public static function invalidFlushMode($mode)
  110. {
  111. return new self("'$mode' is an invalid flush mode.");
  112. }
  113. /**
  114. * @return ORMException
  115. */
  116. public static function entityManagerClosed()
  117. {
  118. return new self("The EntityManager is closed.");
  119. }
  120. /**
  121. * @param string $mode
  122. *
  123. * @return ORMException
  124. */
  125. public static function invalidHydrationMode($mode)
  126. {
  127. return new self("'$mode' is an invalid hydration mode.");
  128. }
  129. /**
  130. * @return ORMException
  131. */
  132. public static function mismatchedEventManager()
  133. {
  134. return new self("Cannot use different EventManager instances for EntityManager and Connection.");
  135. }
  136. /**
  137. * @param string $methodName
  138. *
  139. * @return ORMException
  140. */
  141. public static function findByRequiresParameter($methodName)
  142. {
  143. return new self("You need to pass a parameter to '".$methodName."'");
  144. }
  145. /**
  146. * @param string $entityName
  147. * @param string $fieldName
  148. * @param string $method
  149. *
  150. * @return ORMException
  151. */
  152. public static function invalidFindByCall($entityName, $fieldName, $method)
  153. {
  154. return new self(
  155. "Entity '".$entityName."' has no field '".$fieldName."'. ".
  156. "You can therefore not call '".$method."' on the entities' repository"
  157. );
  158. }
  159. /**
  160. * @param string $entityName
  161. * @param string $associationFieldName
  162. *
  163. * @return ORMException
  164. */
  165. public static function invalidFindByInverseAssociation($entityName, $associationFieldName)
  166. {
  167. return new self(
  168. "You cannot search for the association field '".$entityName."#".$associationFieldName."', ".
  169. "because it is the inverse side of an association. Find methods only work on owning side associations."
  170. );
  171. }
  172. /**
  173. * @return ORMException
  174. */
  175. public static function invalidResultCacheDriver()
  176. {
  177. return new self("Invalid result cache driver; it must implement Doctrine\\Common\\Cache\\Cache.");
  178. }
  179. /**
  180. * @return ORMException
  181. */
  182. public static function notSupported()
  183. {
  184. return new self("This behaviour is (currently) not supported by Doctrine 2");
  185. }
  186. /**
  187. * @return ORMException
  188. */
  189. public static function queryCacheNotConfigured()
  190. {
  191. return new self('Query Cache is not configured.');
  192. }
  193. /**
  194. * @return ORMException
  195. */
  196. public static function metadataCacheNotConfigured()
  197. {
  198. return new self('Class Metadata Cache is not configured.');
  199. }
  200. /**
  201. * @return ORMException
  202. */
  203. public static function proxyClassesAlwaysRegenerating()
  204. {
  205. return new self('Proxy Classes are always regenerating.');
  206. }
  207. /**
  208. * @param string $entityNamespaceAlias
  209. *
  210. * @return ORMException
  211. */
  212. public static function unknownEntityNamespace($entityNamespaceAlias)
  213. {
  214. return new self(
  215. "Unknown Entity namespace alias '$entityNamespaceAlias'."
  216. );
  217. }
  218. /**
  219. * @param string $className
  220. *
  221. * @return ORMException
  222. */
  223. public static function invalidEntityRepository($className)
  224. {
  225. return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository.");
  226. }
  227. /**
  228. * @param string $className
  229. * @param string $fieldName
  230. *
  231. * @return ORMException
  232. */
  233. public static function missingIdentifierField($className, $fieldName)
  234. {
  235. return new self("The identifier $fieldName is missing for a query of " . $className);
  236. }
  237. /**
  238. * @param string $functionName
  239. *
  240. * @return ORMException
  241. */
  242. public static function overwriteInternalDQLFunctionNotAllowed($functionName)
  243. {
  244. return new self("It is not allowed to overwrite internal function '$functionName' in the DQL parser through user-defined functions.");
  245. }
  246. }