MappingException.php 11KB

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