teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2021/06/02 00:22

投稿

accountinitial
accountinitial

スコア0

title CHANGED
File without changes
body CHANGED
@@ -14,7 +14,9 @@
14
14
  ### 追記
15
15
 
16
16
  ```php
17
- // レポートを作成するユースケース
17
+ // レポートを作成するユースケース
18
+ class Interactor
19
+ {
18
20
  public function __construct(ReportRepository $reportRepository)
19
21
  {
20
22
  $this->reportRepository = $reportRepository;
@@ -42,7 +44,9 @@
42
44
  // 永続化
43
45
  $this->reportRepository->create($report);
44
46
  }
47
+ }
45
48
 
49
+ // モデルを永続化するリポジトリ
46
50
  class ChildcareDocumentationRepository
47
51
  {
48
52
  public function __construct(ActivityRepository $activityRepository)

1

追記

2021/06/02 00:22

投稿

accountinitial
accountinitial

スコア0

title CHANGED
File without changes
body CHANGED
@@ -9,4 +9,66 @@
9
9
 
10
10
  ![db](8002bc261a8c7762397d58a37b5a6a21.png)
11
11
 
12
- ![app](37458ec3751c836d565850d19bac5382.png)
12
+ ![app](37458ec3751c836d565850d19bac5382.png)
13
+
14
+ ### 追記
15
+
16
+ ```php
17
+ // レポートを作成するユースケース
18
+ public function __construct(ReportRepository $reportRepository)
19
+ {
20
+ $this->reportRepository = $reportRepository;
21
+ }
22
+
23
+ public function __invoke(InputData $inputData)
24
+ {
25
+ // レポートに紐付けるアクティビティの作成
26
+ $activities = [];
27
+ foreach ($inputData->activitiesWithOrder as $activity) {
28
+ $activities[] = new ActivityWithOrder(
29
+ new ActivityId() // 新規作成時なので、何も指定できない
30
+ ...
31
+ $activity['order']
32
+ );
33
+ }
34
+
35
+ // アクティビティを持つレポートの作成
36
+ $report = new Report(
37
+ new ReportId() // 新規作成時なので、何も指定できない
38
+ ...
39
+ new Activities($activities)
40
+ );
41
+
42
+ // 永続化
43
+ $this->reportRepository->create($report);
44
+ }
45
+
46
+ class ChildcareDocumentationRepository
47
+ {
48
+ public function __construct(ActivityRepository $activityRepository)
49
+ {
50
+ $this->activityRepository = $activityRepository;
51
+ }
52
+
53
+ // リポジトリ
54
+ public function create(Report $report)
55
+ {
56
+ // レポートの永続化
57
+ $daoReport = DaoReport::create([
58
+ ...
59
+ ]);
60
+
61
+ foreach ($report->getActivities() as $activity) {
62
+ // アクティビティの永続化
63
+ $daoActivityId = $this->activityRepository->create($activity);
64
+
65
+ // Activityリポジトリの保存結果で取得されるIDを用いて中間テーブルに保存
66
+ $daoReport->activities()->attach([
67
+ $daoActivityId => $activity->getOrder(),
68
+ ]);
69
+ }
70
+
71
+ return $daoReport->id;
72
+ }
73
+ }
74
+ ```