MappingException.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.phpdoctrine.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping;
  20. /**
  21. * A MappingException indicates that something is wrong with the mapping setup.
  22. *
  23. * @since 2.0
  24. */
  25. class MappingException extends \Doctrine\ORM\ORMException
  26. {
  27. public static function pathRequired()
  28. {
  29. return new self("Specifying the paths to your entities is required ".
  30. "in the AnnotationDriver to retrieve all class names.");
  31. }
  32. public static function identifierRequired($entityName)
  33. {
  34. if (false !== ($parent = get_parent_class($entityName))) {
  35. return new self(sprintf(
  36. 'No identifier/primary key specified for Entity "%s" sub class of "%s". Every Entity must have an identifier/primary key.',
  37. $entityName, $parent
  38. ));
  39. }
  40. return new self(sprintf(
  41. 'No identifier/primary key specified for Entity "%s". Every Entity must have an identifier/primary key.',
  42. $entityName
  43. ));
  44. }
  45. public static function invalidInheritanceType($entityName, $type)
  46. {
  47. return new self("The inheritance type '$type' specified for '$entityName' does not exist.");
  48. }
  49. public static function generatorNotAllowedWithCompositeId()
  50. {
  51. return new self("Id generators can't be used with a composite id.");
  52. }
  53. public static function missingFieldName($entity)
  54. {
  55. return new self("The field or association mapping misses the 'fieldName' attribute in entity '$entity'.");
  56. }
  57. public static function missingTargetEntity($fieldName)
  58. {
  59. return new self("The association mapping '$fieldName' misses the 'targetEntity' attribute.");
  60. }
  61. public static function missingSourceEntity($fieldName)
  62. {
  63. return new self("The association mapping '$fieldName' misses the 'sourceEntity' attribute.");
  64. }
  65. public static function mappingFileNotFound($entityName, $fileName)
  66. {
  67. return new self("No mapping file found named '$fileName' for class '$entityName'.");
  68. }
  69. /**
  70. * Exception for invalid property name override.
  71. *
  72. * @param string $className The entity's name
  73. * @param string $fieldName
  74. */
  75. public static function invalidOverrideFieldName($className, $fieldName)
  76. {
  77. return new self("Invalid field override named '$fieldName' for class '$className'.");
  78. }
  79. /**
  80. * Exception for invalid property type override.
  81. *
  82. * @param string $className The entity's name
  83. * @param string $fieldName
  84. */
  85. public static function invalidOverrideFieldType($className, $fieldName)
  86. {
  87. return new self("The column type of attribute '$fieldName' on class '$className' could not be changed.");
  88. }
  89. public static function mappingNotFound($className, $fieldName)
  90. {
  91. return new self("No mapping found for field '$fieldName' on class '$className'.");
  92. }
  93. public static function queryNotFound($className, $queryName)
  94. {
  95. return new self("No query found named '$queryName' on class '$className'.");
  96. }
  97. public static function resultMappingNotFound($className, $resultName)
  98. {
  99. return new self("No result set mapping found named '$resultName' on class '$className'.");
  100. }
  101. public static function emptyQueryMapping($entity, $queryName)
  102. {
  103. return new self('Query named "'.$queryName.'" in "'.$entity.'" could not be empty.');
  104. }
  105. public static function nameIsMandatoryForQueryMapping($className)
  106. {
  107. return new self("Query name on entity class '$className' is not defined.");
  108. }
  109. public static function missingQueryMapping($entity, $queryName)
  110. {
  111. return new self('Query named "'.$queryName.'" in "'.$entity.' requires a result class or result set mapping.');
  112. }
  113. public static function missingResultSetMappingEntity($entity, $resultName)
  114. {
  115. return new self('Result set mapping named "'.$resultName.'" in "'.$entity.' requires a entity class name.');
  116. }
  117. public static function missingResultSetMappingFieldName($entity, $resultName)
  118. {
  119. return new self('Result set mapping named "'.$resultName.'" in "'.$entity.' requires a field name.');
  120. }
  121. public static function nameIsMandatoryForSqlResultSetMapping($className)
  122. {
  123. return new self("Result set mapping name on entity class '$className' is not defined.");
  124. }
  125. public static function oneToManyRequiresMappedBy($fieldName)
  126. {
  127. return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute.");
  128. }
  129. public static function joinTableRequired($fieldName)
  130. {
  131. return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute.");
  132. }
  133. /**
  134. * Called if a required option was not found but is required
  135. *
  136. * @param string $field which field cannot be processed?
  137. * @param string $expectedOption which option is required
  138. * @param string $hint Can optionally be used to supply a tip for common mistakes,
  139. * e.g. "Did you think of the plural s?"
  140. * @return MappingException
  141. */
  142. static function missingRequiredOption($field, $expectedOption, $hint = '')
  143. {
  144. $message = "The mapping of field '{$field}' is invalid: The option '{$expectedOption}' is required.";
  145. if ( ! empty($hint)) {
  146. $message .= ' (Hint: ' . $hint . ')';
  147. }
  148. return new self($message);
  149. }
  150. /**
  151. * Generic exception for invalid mappings.
  152. *
  153. * @param string $fieldName
  154. */
  155. public static function invalidMapping($fieldName)
  156. {
  157. return new self("The mapping of field '$fieldName' is invalid.");
  158. }
  159. /**
  160. * Exception for reflection exceptions - adds the entity name,
  161. * because there might be long classnames that will be shortened
  162. * within the stacktrace
  163. *
  164. * @param string $entity The entity's name
  165. * @param \ReflectionException $previousException
  166. */
  167. public static function reflectionFailure($entity, \ReflectionException $previousException)
  168. {
  169. return new self('An error occurred in ' . $entity, 0, $previousException);
  170. }
  171. public static function joinColumnMustPointToMappedField($className, $joinColumn)
  172. {
  173. return new self('The column ' . $joinColumn . ' must be mapped to a field in class '
  174. . $className . ' since it is referenced by a join column of another class.');
  175. }
  176. public static function classIsNotAValidEntityOrMappedSuperClass($className)
  177. {
  178. if (false !== ($parent = get_parent_class($className))) {
  179. return new self(sprintf(
  180. 'Class "%s" sub class of "%s" is not a valid entity or mapped super class.',
  181. $className, $parent
  182. ));
  183. }
  184. return new self(sprintf(
  185. 'Class "%s" is not a valid entity or mapped super class.',
  186. $className
  187. ));
  188. }
  189. public static function propertyTypeIsRequired($className, $propertyName)
  190. {
  191. return new self("The attribute 'type' is required for the column description of property ".$className."::\$".$propertyName.".");
  192. }
  193. public static function tableIdGeneratorNotImplemented($className)
  194. {
  195. return new self("TableIdGenerator is not yet implemented for use with class ".$className);
  196. }
  197. /**
  198. * @param string $entity The entity's name
  199. * @param string $fieldName The name of the field that was already declared
  200. */
  201. public static function duplicateFieldMapping($entity, $fieldName)
  202. {
  203. return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  204. }
  205. public static function duplicateAssociationMapping($entity, $fieldName)
  206. {
  207. return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  208. }
  209. public static function duplicateQueryMapping($entity, $queryName)
  210. {
  211. return new self('Query named "'.$queryName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  212. }
  213. public static function duplicateResultSetMapping($entity, $resultName)
  214. {
  215. return new self('Result set mapping named "'.$resultName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  216. }
  217. public static function singleIdNotAllowedOnCompositePrimaryKey($entity)
  218. {
  219. return new self('Single id is not allowed on composite primary key in entity '.$entity);
  220. }
  221. public static function unsupportedOptimisticLockingType($entity, $fieldName, $unsupportedType)
  222. {
  223. return new self('Locking type "'.$unsupportedType.'" (specified in "'.$entity.'", field "'.$fieldName.'") '
  224. .'is not supported by Doctrine.'
  225. );
  226. }
  227. public static function fileMappingDriversRequireConfiguredDirectoryPath($path = null)
  228. {
  229. if ( ! empty($path)) {
  230. $path = '[' . $path . ']';
  231. }
  232. return new self(
  233. 'File mapping drivers must have a valid directory path, ' .
  234. 'however the given path ' . $path . ' seems to be incorrect!'
  235. );
  236. }
  237. /**
  238. * Throws an exception that indicates that a class used in a discriminator map does not exist.
  239. * An example would be an outdated (maybe renamed) classname.
  240. *
  241. * @param string $className The class that could not be found
  242. * @param string $owningClass The class that declares the discriminator map.
  243. * @return self
  244. */
  245. public static function invalidClassInDiscriminatorMap($className, $owningClass)
  246. {
  247. return new self(
  248. "Entity class '$className' used in the discriminator map of class '$owningClass' ".
  249. "does not exist."
  250. );
  251. }
  252. public static function duplicateDiscriminatorEntry($className, array $entries, array $map)
  253. {
  254. return new self(
  255. "The entries " . implode(', ', $entries) . " in discriminator map of class '" . $className . "' is duplicated. " .
  256. "If the discriminator map is automatically generated you have to convert it to an explicit discriminator map now. " .
  257. "The entries of the current map are: @DiscriminatorMap({" . implode(', ', array_map(
  258. function($a, $b) { return "'$a': '$b'"; }, array_keys($map), array_values($map)
  259. )) . "})"
  260. );
  261. }
  262. public static function missingDiscriminatorMap($className)
  263. {
  264. return new self("Entity class '$className' is using inheritance but no discriminator map was defined.");
  265. }
  266. public static function missingDiscriminatorColumn($className)
  267. {
  268. return new self("Entity class '$className' is using inheritance but no discriminator column was defined.");
  269. }
  270. public static function invalidDiscriminatorColumnType($className, $type)
  271. {
  272. return new self("Discriminator column type on entity class '$className' is not allowed to be '$type'. 'string' or 'integer' type variables are suggested!");
  273. }
  274. public static function nameIsMandatoryForDiscriminatorColumns($className)
  275. {
  276. return new self("Discriminator column name on entity class '$className' is not defined.");
  277. }
  278. public static function cannotVersionIdField($className, $fieldName)
  279. {
  280. return new self("Setting Id field '$fieldName' as versionale in entity class '$className' is not supported.");
  281. }
  282. public static function sqlConversionNotAllowedForIdentifiers($className, $fieldName, $type)
  283. {
  284. return new self("It is not possible to set id field '$fieldName' to type '$type' in entity class '$className'. The type '$type' requires conversion SQL which is not allowed for identifiers.");
  285. }
  286. /**
  287. * @param string $className
  288. * @param string $columnName
  289. * @return self
  290. */
  291. public static function duplicateColumnName($className, $columnName)
  292. {
  293. return new self("Duplicate definition of column '".$columnName."' on entity '".$className."' in a field or discriminator column mapping.");
  294. }
  295. public static function illegalToManyAssocationOnMappedSuperclass($className, $field)
  296. {
  297. return new self("It is illegal to put an inverse side one-to-many or many-to-many association on mapped superclass '".$className."#".$field."'.");
  298. }
  299. /**
  300. * @param string $className
  301. * @param string $targetEntity
  302. * @param string $targetField
  303. * @return self
  304. */
  305. public static function cannotMapCompositePrimaryKeyEntitiesAsForeignId($className, $targetEntity, $targetField)
  306. {
  307. return new self("It is not possible to map entity '".$className."' with a composite primary key ".
  308. "as part of the primary key of another entity '".$targetEntity."#".$targetField."'.");
  309. }
  310. public static function noSingleAssociationJoinColumnFound($className, $field)
  311. {
  312. return new self("'$className#$field' is not an association with a single join column.");
  313. }
  314. public static function noFieldNameFoundForColumn($className, $column)
  315. {
  316. return new self("Cannot find a field on '$className' that is mapped to column '$column'. Either the ".
  317. "field does not exist or an association exists but it has multiple join columns.");
  318. }
  319. public static function illegalOrphanRemovalOnIdentifierAssociation($className, $field)
  320. {
  321. return new self("The orphan removal option is not allowed on an association that is ".
  322. "part of the identifier in '$className#$field'.");
  323. }
  324. public static function illegalOrphanRemoval($className, $field)
  325. {
  326. return new self("Orphan removal is only allowed on one-to-one and one-to-many ".
  327. "associations, but " . $className."#" .$field . " is not.");
  328. }
  329. public static function illegalInverseIdentifierAssocation($className, $field)
  330. {
  331. return new self("An inverse association is not allowed to be identifier in '$className#$field'.");
  332. }
  333. public static function illegalToManyIdentifierAssoaction($className, $field)
  334. {
  335. return new self("Many-to-many or one-to-many associations are not allowed to be identifier in '$className#$field'.");
  336. }
  337. public static function noInheritanceOnMappedSuperClass($className)
  338. {
  339. return new self("Its not supported to define inheritance information on a mapped superclass '" . $className . "'.");
  340. }
  341. public static function mappedClassNotPartOfDiscriminatorMap($className, $rootClassName)
  342. {
  343. return new self(
  344. "Entity '" . $className . "' has to be part of the discriminator map of '" . $rootClassName . "' " .
  345. "to be properly mapped in the inheritance hierachy. Alternatively you can make '".$className."' an abstract class " .
  346. "to avoid this exception from occuring."
  347. );
  348. }
  349. public static function lifecycleCallbackMethodNotFound($className, $methodName)
  350. {
  351. return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback.");
  352. }
  353. public static function invalidFetchMode($className, $annotation)
  354. {
  355. return new self("Entity '" . $className . "' has a mapping with invalid fetch mode '" . $annotation . "'");
  356. }
  357. public static function compositeKeyAssignedIdGeneratorRequired($className)
  358. {
  359. return new self("Entity '". $className . "' has a composite identifier but uses an ID generator other than manually assigning (Identity, Sequence). This is not supported.");
  360. }
  361. public static function invalidTargetEntityClass($targetEntity, $sourceEntity, $associationName)
  362. {
  363. return new self("The target-entity " . $targetEntity . " cannot be found in '" . $sourceEntity."#".$associationName."'.");
  364. }
  365. public static function invalidCascadeOption(array $cascades, $className, $propertyName)
  366. {
  367. $cascades = implode(", ", array_map(function ($e) { return "'" . $e . "'"; }, $cascades));
  368. return new self(sprintf(
  369. "You have specified invalid cascade options for %s::$%s: %s; available options: 'remove', 'persist', 'refresh', 'merge', and 'detach'",
  370. $className,
  371. $propertyName,
  372. $cascades
  373. ));
  374. }
  375. }