ORMException.php 9.6 KB

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