Exceptions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * Generic base exception for all ActiveRecord specific errors.
  8. *
  9. * @package ActiveRecord
  10. */
  11. class ActiveRecordException extends \Exception {};
  12. /**
  13. * Thrown when a record cannot be found.
  14. *
  15. * @package ActiveRecord
  16. */
  17. class RecordNotFound extends ActiveRecordException {};
  18. /**
  19. * Thrown when there was an error performing a database operation.
  20. *
  21. * The error will be specific to whatever database you are running.
  22. *
  23. * @package ActiveRecord
  24. */
  25. class DatabaseException extends ActiveRecordException
  26. {
  27. public function __construct($adapter_or_string_or_mystery)
  28. {
  29. if ($adapter_or_string_or_mystery instanceof Connection)
  30. {
  31. parent::__construct(
  32. join(", ",$adapter_or_string_or_mystery->connection->errorInfo()),
  33. intval($adapter_or_string_or_mystery->connection->errorCode()));
  34. }
  35. elseif ($adapter_or_string_or_mystery instanceof \PDOStatement)
  36. {
  37. parent::__construct(
  38. join(", ",$adapter_or_string_or_mystery->errorInfo()),
  39. intval($adapter_or_string_or_mystery->errorCode()));
  40. }
  41. else
  42. parent::__construct($adapter_or_string_or_mystery);
  43. }
  44. };
  45. /**
  46. * Thrown by {@link Model}.
  47. *
  48. * @package ActiveRecord
  49. */
  50. class ModelException extends ActiveRecordException {};
  51. /**
  52. * Thrown by {@link Expressions}.
  53. *
  54. * @package ActiveRecord
  55. */
  56. class ExpressionsException extends ActiveRecordException {};
  57. /**
  58. * Thrown for configuration problems.
  59. *
  60. * @package ActiveRecord
  61. */
  62. class ConfigException extends ActiveRecordException {};
  63. /**
  64. * Thrown when attempting to access an invalid property on a {@link Model}.
  65. *
  66. * @package ActiveRecord
  67. */
  68. class UndefinedPropertyException extends ModelException
  69. {
  70. /**
  71. * Sets the exception message to show the undefined property's name.
  72. *
  73. * @param str $property_name name of undefined property
  74. * @return void
  75. */
  76. public function __construct($class_name, $property_name)
  77. {
  78. if (is_array($property_name))
  79. {
  80. $this->message = implode("\r\n", $property_name);
  81. return;
  82. }
  83. $this->message = "Undefined property: {$class_name}->{$property_name} in {$this->file} on line {$this->line}";
  84. parent::__construct();
  85. }
  86. };
  87. /**
  88. * Thrown when attempting to perform a write operation on a {@link Model} that is in read-only mode.
  89. *
  90. * @package ActiveRecord
  91. */
  92. class ReadOnlyException extends ModelException
  93. {
  94. /**
  95. * Sets the exception message to show the undefined property's name.
  96. *
  97. * @param str $class_name name of the model that is read only
  98. * @param str $method_name name of method which attempted to modify the model
  99. * @return void
  100. */
  101. public function __construct($class_name, $method_name)
  102. {
  103. $this->message = "{$class_name}::{$method_name}() cannot be invoked because this model is set to read only";
  104. parent::__construct();
  105. }
  106. };
  107. /**
  108. * Thrown for validations exceptions.
  109. *
  110. * @package ActiveRecord
  111. */
  112. class ValidationsArgumentError extends ActiveRecordException {};
  113. /**
  114. * Thrown for relationship exceptions.
  115. *
  116. * @package ActiveRecord
  117. */
  118. class RelationshipException extends ActiveRecordException {};
  119. /**
  120. * Thrown for has many thru exceptions.
  121. *
  122. * @package ActiveRecord
  123. */
  124. class HasManyThroughAssociationException extends RelationshipException {};
  125. ?>