MappingException.php 4.1 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
romanb's avatar
romanb committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.org>.
 */

22
namespace Doctrine\ORM\Mapping;
romanb's avatar
romanb committed
23 24 25 26 27 28

/**
 * A MappingException indicates that something is wrong with the mapping setup.
 *
 * @since 2.0
 */
29
class MappingException extends \Doctrine\ORM\ORMException
romanb's avatar
romanb committed
30 31 32
{
    public static function identifierRequired($entityName)
    {
romanb's avatar
romanb committed
33 34
        return new self("No identifier/primary key specified for Entity '$entityName'."
                . " Every Entity must have an identifier/primary key.");
romanb's avatar
romanb committed
35
    }
36 37 38 39 40 41 42 43 44 45 46
    
    public static function invalidInheritanceType($type)
    {
        return new self("The inheritance type '$type' does not exist.");
    }
    
    public static function generatorNotAllowedWithCompositeId()
    {
        return new self("Id generators can't be used with a composite id.");
    }
    
romanb's avatar
romanb committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    public static function missingFieldName()
    {
        return new self("The association mapping misses the 'fieldName' attribute.");
    }
    
    public static function missingTargetEntity($fieldName)
    {
        return new self("The association mapping '$fieldName' misses the 'targetEntity' attribute.");
    }
    
    public static function missingSourceEntity($fieldName)
    {
        return new self("The association mapping '$fieldName' misses the 'sourceEntity' attribute.");
    }
    
    public static function mappingNotFound($fieldName)
    {
        return new self("No mapping found for field '$fieldName'.");
    }
66 67 68 69 70
    
    public static function oneToManyRequiresMappedBy($fieldName)
    {
        return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute.");
    }
romanb's avatar
romanb committed
71 72 73 74 75 76
    
    public static function joinTableRequired($fieldName)
    {
        return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute.");
    }
    
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    /**
     * Called if a required option was not found but is required
     * 
     * @param string $field which field cannot be processed?
     * @param string $expectedOption which option is required
     * @param string $hint  Can optionally be used to supply a tip for common mistakes, 
     *                      e.g. "Did you think of the plural s?"
     * @return MappingException 
     */
    static function missingRequiredOption($field, $expectedOption, $hint = '') 
    {
        $message = "The mapping of field '{$field}' is invalid: The option '{$expectedOption}' is required.";

        if ( ! empty($hint)) {
            $message .= ' (Hint: ' . $hint . ')';
        }

        return new self($message);
    }
    
romanb's avatar
romanb committed
97 98 99 100 101 102 103 104 105
    /**
     * Generic exception for invalid mappings.
     *
     * @param string $fieldName
     */
    public static function invalidMapping($fieldName)
    {
        return new self("The mapping of field '$fieldName' is invalid.");
    }
106 107 108 109 110 111 112 113 114
    
    /**
     * Exception for reflection exceptions - adds the entity name,
     * because there might be long classnames that will be shortened
     * within the stacktrace
     * 
     * @param string $entity The entity's name
     * @param \ReflectionException $previousException
     */
115 116 117
    public static function reflectionFailure($entity, \ReflectionException $previousException)
    {
        return new self('An error occurred in ' . $entity, 0, $previousException);
118
    }
119
}