TestRepository.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Module\Transaction\Repositorys;
  3. use App\Module\Test\Models\Test;
  4. class TestRepository
  5. {
  6. protected Test $model;
  7. public function __construct(Test $model)
  8. {
  9. $this->model = $model;
  10. }
  11. /**
  12. * 查找记录
  13. *
  14. * @param int $id
  15. * @return Test|null
  16. */
  17. public function find(int $id): ?Test
  18. {
  19. return $this->model->find($id);
  20. }
  21. /**
  22. * 创建记录
  23. *
  24. * @param array $data
  25. * @return Test
  26. */
  27. public function create(array $data): Test
  28. {
  29. return $this->model->create($data);
  30. }
  31. /**
  32. * 更新记录
  33. *
  34. * @param int $id
  35. * @param array $data
  36. * @return bool
  37. */
  38. public function update(int $id, array $data): bool
  39. {
  40. $model = $this->find($id);
  41. if (!$model) {
  42. return false;
  43. }
  44. return $model->update($data);
  45. }
  46. /**
  47. * 删除记录
  48. *
  49. * @param int $id
  50. * @return bool
  51. */
  52. public function delete(int $id): bool
  53. {
  54. $model = $this->find($id);
  55. if (!$model) {
  56. return false;
  57. }
  58. return $model->delete();
  59. }
  60. }