MappingException.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 LGPL. 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. return new self("No identifier/primary key specified for Entity '$entityName'."
  35. . " Every Entity must have an identifier/primary key.");
  36. }
  37. public static function invalidInheritanceType($entityName, $type)
  38. {
  39. return new self("The inheritance type '$type' specified for '$entityName' does not exist.");
  40. }
  41. public static function generatorNotAllowedWithCompositeId()
  42. {
  43. return new self("Id generators can't be used with a composite id.");
  44. }
  45. public static function missingFieldName($entity)
  46. {
  47. return new self("The field or association mapping misses the 'fieldName' attribute in entity '$entity'.");
  48. }
  49. public static function missingTargetEntity($fieldName)
  50. {
  51. return new self("The association mapping '$fieldName' misses the 'targetEntity' attribute.");
  52. }
  53. public static function missingSourceEntity($fieldName)
  54. {
  55. return new self("The association mapping '$fieldName' misses the 'sourceEntity' attribute.");
  56. }
  57. public static function mappingFileNotFound($entityName, $fileName)
  58. {
  59. return new self("No mapping file found named '$fileName' for class '$entityName'.");
  60. }
  61. public static function invalidMappingFile($entityName, $fileName)
  62. {
  63. return new self("Invalid mapping file '$fileName' for class '$entityName'.");
  64. }
  65. public static function mappingNotFound($className, $fieldName)
  66. {
  67. return new self("No mapping found for field '$fieldName' on class '$className'.");
  68. }
  69. public static function queryNotFound($className, $queryName)
  70. {
  71. return new self("No query found named '$queryName' on class '$className'.");
  72. }
  73. public static function oneToManyRequiresMappedBy($fieldName)
  74. {
  75. return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute.");
  76. }
  77. public static function joinTableRequired($fieldName)
  78. {
  79. return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute.");
  80. }
  81. /**
  82. * Called if a required option was not found but is required
  83. *
  84. * @param string $field which field cannot be processed?
  85. * @param string $expectedOption which option is required
  86. * @param string $hint Can optionally be used to supply a tip for common mistakes,
  87. * e.g. "Did you think of the plural s?"
  88. * @return MappingException
  89. */
  90. static function missingRequiredOption($field, $expectedOption, $hint = '')
  91. {
  92. $message = "The mapping of field '{$field}' is invalid: The option '{$expectedOption}' is required.";
  93. if ( ! empty($hint)) {
  94. $message .= ' (Hint: ' . $hint . ')';
  95. }
  96. return new self($message);
  97. }
  98. /**
  99. * Generic exception for invalid mappings.
  100. *
  101. * @param string $fieldName
  102. */
  103. public static function invalidMapping($fieldName)
  104. {
  105. return new self("The mapping of field '$fieldName' is invalid.");
  106. }
  107. /**
  108. * Exception for reflection exceptions - adds the entity name,
  109. * because there might be long classnames that will be shortened
  110. * within the stacktrace
  111. *
  112. * @param string $entity The entity's name
  113. * @param \ReflectionException $previousException
  114. */
  115. public static function reflectionFailure($entity, \ReflectionException $previousException)
  116. {
  117. return new self('An error occurred in ' . $entity, 0, $previousException);
  118. }
  119. public static function joinColumnMustPointToMappedField($className, $joinColumn)
  120. {
  121. return new self('The column ' . $joinColumn . ' must be mapped to a field in class '
  122. . $className . ' since it is referenced by a join column of another class.');
  123. }
  124. public static function classIsNotAValidEntityOrMappedSuperClass($className)
  125. {
  126. return new self('Class '.$className.' is not a valid entity or mapped super class.');
  127. }
  128. public static function propertyTypeIsRequired($className, $propertyName)
  129. {
  130. return new self("The attribute 'type' is required for the column description of property ".$className."::\$".$propertyName.".");
  131. }
  132. public static function tableIdGeneratorNotImplemented($className)
  133. {
  134. return new self("TableIdGenerator is not yet implemented for use with class ".$className);
  135. }
  136. /**
  137. *
  138. * @param string $entity The entity's name
  139. * @param string $fieldName The name of the field that was already declared
  140. */
  141. public static function duplicateFieldMapping($entity, $fieldName) {
  142. return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  143. }
  144. public static function duplicateAssociationMapping($entity, $fieldName) {
  145. return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  146. }
  147. public static function duplicateQueryMapping($entity, $queryName) {
  148. return new self('Query named "'.$queryName.'" in "'.$entity.'" was already declared, but it must be declared only once');
  149. }
  150. public static function singleIdNotAllowedOnCompositePrimaryKey($entity) {
  151. return new self('Single id is not allowed on composite primary key in entity '.$entity);
  152. }
  153. public static function unsupportedOptimisticLockingType($entity, $fieldName, $unsupportedType) {
  154. return new self('Locking type "'.$unsupportedType.'" (specified in "'.$entity.'", field "'.$fieldName.'") '
  155. .'is not supported by Doctrine.'
  156. );
  157. }
  158. public static function fileMappingDriversRequireConfiguredDirectoryPath($path = null)
  159. {
  160. if ( ! empty($path)) {
  161. $path = '[' . $path . ']';
  162. }
  163. return new self(
  164. 'File mapping drivers must have a valid directory path, ' .
  165. 'however the given path ' . $path . ' seems to be incorrect!'
  166. );
  167. }
  168. /**
  169. * Throws an exception that indicates that a class used in a discriminator map does not exist.
  170. * An example would be an outdated (maybe renamed) classname.
  171. *
  172. * @param string $className The class that could not be found
  173. * @param string $owningClass The class that declares the discriminator map.
  174. * @return self
  175. */
  176. public static function invalidClassInDiscriminatorMap($className, $owningClass) {
  177. return new self(
  178. "Entity class '$className' used in the discriminator map of class '$owningClass' ".
  179. "does not exist."
  180. );
  181. }
  182. public static function missingDiscriminatorMap($className)
  183. {
  184. return new self("Entity class '$className' is using inheritance but no discriminator map was defined.");
  185. }
  186. public static function missingDiscriminatorColumn($className)
  187. {
  188. return new self("Entity class '$className' is using inheritance but no discriminator column was defined.");
  189. }
  190. public static function invalidDiscriminatorColumnType($className, $type)
  191. {
  192. return new self("Discriminator column type on entity class '$className' is not allowed to be '$type'. 'string' or 'integer' type variables are suggested!");
  193. }
  194. public static function cannotVersionIdField($className, $fieldName)
  195. {
  196. return new self("Setting Id field '$fieldName' as versionale in entity class '$className' is not supported.");
  197. }
  198. public static function sqlConversionNotAllowedForIdentifiers($className, $fieldName, $type)
  199. {
  200. 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.");
  201. }
  202. /**
  203. * @param string $className
  204. * @param string $columnName
  205. * @return self
  206. */
  207. public static function duplicateColumnName($className, $columnName)
  208. {
  209. return new self("Duplicate definition of column '".$columnName."' on entity '".$className."' in a field or discriminator column mapping.");
  210. }
  211. public static function illegalToManyAssocationOnMappedSuperclass($className, $field)
  212. {
  213. return new self("It is illegal to put an inverse side one-to-many or many-to-many association on mapped superclass '".$className."#".$field."'.");
  214. }
  215. /**
  216. * @param string $className
  217. * @param string $targetEntity
  218. * @param string $targetField
  219. * @return self
  220. */
  221. public static function cannotMapCompositePrimaryKeyEntitiesAsForeignId($className, $targetEntity, $targetField)
  222. {
  223. return new self("It is not possible to map entity '".$className."' with a composite primary key ".
  224. "as part of the primary key of another entity '".$targetEntity."#".$targetField."'.");
  225. }
  226. public static function noSingleAssociationJoinColumnFound($className, $field)
  227. {
  228. return new self("'$className#$field' is not an association with a single join column.");
  229. }
  230. public static function noFieldNameFoundForColumn($className, $column)
  231. {
  232. return new self("Cannot find a field on '$className' that is mapped to column '$column'. Either the ".
  233. "field does not exist or an association exists but it has multiple join columns.");
  234. }
  235. public static function illegalOrphanRemovalOnIdentifierAssociation($className, $field)
  236. {
  237. return new self("The orphan removal option is not allowed on an association that is ".
  238. "part of the identifier in '$className#$field'.");
  239. }
  240. public static function illegalOrphanRemoval($className, $field)
  241. {
  242. return new self("Orphan removal is only allowed on one-to-one and one-to-many ".
  243. "associations, but " . $className."#" .$field . " is not.");
  244. }
  245. public static function illegalInverseIdentifierAssocation($className, $field)
  246. {
  247. return new self("An inverse association is not allowed to be identifier in '$className#$field'.");
  248. }
  249. public static function illegalToManyIdentifierAssoaction($className, $field)
  250. {
  251. return new self("Many-to-many or one-to-many associations are not allowed to be identifier in '$className#$field'.");
  252. }
  253. public static function noInheritanceOnMappedSuperClass($className)
  254. {
  255. return new self("Its not supported to define inheritance information on a mapped superclass '" . $className . "'.");
  256. }
  257. public static function mappedClassNotPartOfDiscriminatorMap($className, $rootClassName)
  258. {
  259. return new self(
  260. "Entity '" . $className . "' has to be part of the discriminator map of '" . $rootClassName . "' " .
  261. "to be properly mapped in the inheritance hierachy. Alternatively you can make '".$className."' an abstract class " .
  262. "to avoid this exception from occuring."
  263. );
  264. }
  265. public static function lifecycleCallbackMethodNotFound($className, $methodName)
  266. {
  267. return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback.");
  268. }
  269. public static function invalidFetchMode($className, $annotation)
  270. {
  271. return new self("Entity '" . $className . "' has a mapping with invalid fetch mode '" . $annotation . "'");
  272. }
  273. public static function compositeKeyAssignedIdGeneratorRequired($className)
  274. {
  275. return new self("Entity '". $className . "' has a composite identifier but uses an ID generator other than manually assigning (Identity, Sequence). This is not supported.");
  276. }
  277. public static function invalidTargetEntityClass($targetEntity, $sourceEntity, $associationName)
  278. {
  279. return new self("The target-entity " . $targetEntity . " cannot be found in '" . $sourceEntity."#".$associationName."'.");
  280. }
  281. }