ORMInvalidArgumentException.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /**
  21. * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
  22. *
  23. * @author Benjamin Eberlei <kontakt@beberlei.de>
  24. */
  25. class ORMInvalidArgumentException extends \InvalidArgumentException
  26. {
  27. static public function scheduleInsertForManagedEntity($entity)
  28. {
  29. return new self("A managed+dirty entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
  30. }
  31. static public function scheduleInsertForRemovedEntity($entity)
  32. {
  33. return new self("Removed entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
  34. }
  35. static public function scheduleInsertTwice($entity)
  36. {
  37. return new self("Entity " . self::objToStr($entity) . " can not be scheduled for insertion twice.");
  38. }
  39. static public function entityWithoutIdentity($className, $entity)
  40. {
  41. return new self(
  42. "The given entity of type '" . $className . "' (".self::objToStr($entity).") has no identity/no " .
  43. "id values set. It cannot be added to the identity map."
  44. );
  45. }
  46. static public function readOnlyRequiresManagedEntity($entity)
  47. {
  48. return new self("Only managed entities can be marked or checked as read only. But " . self::objToStr($entity) . " is not");
  49. }
  50. static public function newEntityFoundThroughRelationship(array $assoc, $entry)
  51. {
  52. return new self("A new entity was found through the relationship '"
  53. . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not"
  54. . " configured to cascade persist operations for entity: " . self::objToStr($entry) . "."
  55. . " To solve this issue: Either explicitly call EntityManager#persist()"
  56. . " on this unknown entity or configure cascade persist "
  57. . " this association in the mapping for example @ManyToOne(..,cascade={\"persist\"})."
  58. . (method_exists($entry, '__toString') ?
  59. "":
  60. " If you cannot find out which entity causes the problem"
  61. ." implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue."));
  62. }
  63. static public function detachedEntityFoundThroughRelationship(array $assoc, $entry)
  64. {
  65. return new self("A detached entity of type " . $assoc['targetEntity'] . " (" . self::objToStr($entry) . ") "
  66. . " was found through the relationship '" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' "
  67. . "during cascading a persist operation.");
  68. }
  69. static public function entityNotManaged($entity)
  70. {
  71. return new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " .
  72. "from the database or registered as new through EntityManager#persist");
  73. }
  74. static public function entityHasNoIdentity($entity, $operation)
  75. {
  76. return new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  77. }
  78. static public function entityIsRemoved($entity, $operation)
  79. {
  80. return new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  81. }
  82. static public function detachedEntityCannot($entity, $operation)
  83. {
  84. return new self("A detached entity was found during " . $operation . " " . self::objToStr($entity));
  85. }
  86. public static function invalidObject($context, $given, $parameterIndex = 1)
  87. {
  88. return new self($context .' expects parameter ' . $parameterIndex .
  89. ' to be an entity object, '. gettype($given) . ' given.');
  90. }
  91. /**
  92. * Helper method to show an object as string.
  93. *
  94. * @param object $obj
  95. * @return string
  96. */
  97. private static function objToStr($obj)
  98. {
  99. return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj);
  100. }
  101. }