ModelNotFoundException.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Model;
  12. use Hyperf\Collection\Arr;
  13. use RuntimeException;
  14. class ModelNotFoundException extends RuntimeException
  15. {
  16. /**
  17. * Name of the affected Model model.
  18. */
  19. protected ?string $model = null;
  20. /**
  21. * The affected model IDs.
  22. */
  23. protected array $ids = [];
  24. /**
  25. * Set the affected Model model and instance ids.
  26. *
  27. * @param array|int|string $ids
  28. * @return $this
  29. */
  30. public function setModel(string $model, $ids = [])
  31. {
  32. $this->model = $model;
  33. $this->ids = Arr::wrap($ids);
  34. $this->message = "No query results for model [{$model}]";
  35. if (count($this->ids) > 0) {
  36. $this->message .= ' ' . implode(', ', $this->ids);
  37. } else {
  38. $this->message .= '.';
  39. }
  40. return $this;
  41. }
  42. /**
  43. * Get the affected Model model.
  44. */
  45. public function getModel(): ?string
  46. {
  47. return $this->model;
  48. }
  49. /**
  50. * Get the affected Model model IDs.
  51. */
  52. public function getIds(): array
  53. {
  54. return $this->ids;
  55. }
  56. }