回答編集履歴

16

fill this id

2017/02/26 14:01

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -98,6 +98,8 @@
98
98
 
99
99
  $revision->fill(['unique_id' => $latest->id])->save();
100
100
 
101
+ $this->id = $latest->id;
102
+
101
103
  }
102
104
 
103
105
  if ($this->usesTimestamps()) {

15

fill

2017/02/26 14:01

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -96,9 +96,7 @@
96
96
 
97
97
  if (!$exists) {
98
98
 
99
- $revision->unique_id = $latest->id;
99
+ $revision->fill(['unique_id' => $latest->id])->save();
100
-
101
- $revision->save();
102
100
 
103
101
  }
104
102
 

14

refactor

2017/02/26 13:54

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -52,75 +52,77 @@
52
52
 
53
53
  ```php
54
54
 
55
+ protected $guarded = ['id', 'created_at', 'updated_at'];
56
+
57
+
58
+
55
59
  public function save(array $options = [])
56
60
 
57
61
  {
58
62
 
59
- if ($this->isDirty()) {
63
+ if (!$this->isDirty()) {
64
+
65
+ return true;
66
+
67
+ }
60
68
 
61
69
 
62
70
 
63
- DB::transaction(function () use ($options) {
71
+ return DB::transaction(function () use ($options) {
72
+
73
+ $exists = $this->exists;
64
74
 
65
75
 
66
76
 
67
- $excepts = [
77
+ if ($exists && $this->fireModelEvent('updating') === false) {
68
78
 
69
- $this->getKeyName(),
79
+ return false;
70
80
 
71
- $this->getCreatedAtColumn(),
81
+ }
72
82
 
73
- $this->getUpdatedAtColumn(),
83
+ if (!$exists && $this->fireModelEvent('creating') === false) {
74
84
 
75
- ];
85
+ return false;
76
86
 
77
- $revision = Models\StaffRevision::create(
78
-
79
- ['unique_id' => $this->id]
80
-
81
- + Arr::except($this->getAttributes(), $excepts)
82
-
83
- );
87
+ }
84
-
85
- $latest = Models\StaffLatest::where('id', $this->id)
86
-
87
- ->updateOrCreate(['revision_id' => $revision->id]);
88
88
 
89
89
 
90
90
 
91
- if (!$this->exists) {
91
+ $revision = Models\StaffRevision::create(['unique_id' => $this->id] + $this->getFillable());
92
92
 
93
- $revision->unique_id = $latest->id;
94
-
95
- $revision->save();
96
-
97
- }
98
-
99
- if ($this->usesTimestamps()) {
100
-
101
- $this->updateTimestamps();
102
-
103
- }
104
-
105
- if (!$this->exists) {
106
-
107
- $this->exists = true;
108
-
109
- $this->wasRecentlyCreated = true;
110
-
111
- $this->fireModelEvent('created', false);
93
+ $latest = Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $revision->id]);
112
-
113
- $this->finishSave($options);
114
-
115
- }
116
94
 
117
95
 
118
96
 
119
- });
97
+ if (!$exists) {
120
98
 
121
- }
99
+ $revision->unique_id = $latest->id;
122
100
 
101
+ $revision->save();
102
+
103
+ }
104
+
105
+ if ($this->usesTimestamps()) {
106
+
107
+ $this->updateTimestamps();
108
+
109
+ }
110
+
111
+
112
+
113
+ $this->exists = true;
114
+
115
+ $this->wasRecentlyCreated = !$exists;
116
+
117
+ $this->fireModelEvent($exists ? 'updated' : 'created', false);
118
+
119
+ $this->finishSave($options);
120
+
121
+
122
+
123
- return true;
123
+ return true;
124
+
125
+ });
124
126
 
125
127
  }
126
128
 

13

fix

2017/02/26 13:46

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -58,7 +58,11 @@
58
58
 
59
59
  if ($this->isDirty()) {
60
60
 
61
+
62
+
61
63
  DB::transaction(function () use ($options) {
64
+
65
+
62
66
 
63
67
  $excepts = [
64
68
 
@@ -70,15 +74,35 @@
70
74
 
71
75
  ];
72
76
 
73
- $revision = Models\StaffRevision::create(['unique_id' => $this->id] + Arr::except($this->getAttributes(), $excepts));
77
+ $revision = Models\StaffRevision::create(
74
78
 
79
+ ['unique_id' => $this->id]
80
+
81
+ + Arr::except($this->getAttributes(), $excepts)
82
+
83
+ );
84
+
85
+ $latest = Models\StaffLatest::where('id', $this->id)
86
+
75
- $latest = Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $revision->id]);
87
+ ->updateOrCreate(['revision_id' => $revision->id]);
88
+
89
+
76
90
 
77
91
  if (!$this->exists) {
78
92
 
79
93
  $revision->unique_id = $latest->id;
80
94
 
81
95
  $revision->save();
96
+
97
+ }
98
+
99
+ if ($this->usesTimestamps()) {
100
+
101
+ $this->updateTimestamps();
102
+
103
+ }
104
+
105
+ if (!$this->exists) {
82
106
 
83
107
  $this->exists = true;
84
108
 
@@ -90,6 +114,8 @@
90
114
 
91
115
  }
92
116
 
117
+
118
+
93
119
  });
94
120
 
95
121
  }

12

rename new

2017/02/26 13:11

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -76,9 +76,9 @@
76
76
 
77
77
  if (!$this->exists) {
78
78
 
79
- $new->unique_id = $latest->id;
79
+ $revision->unique_id = $latest->id;
80
80
 
81
- $new->save();
81
+ $revision->save();
82
82
 
83
83
  $this->exists = true;
84
84
 

11

fix

2017/02/26 12:50

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -70,13 +70,13 @@
70
70
 
71
71
  ];
72
72
 
73
- $new = Models\StaffRevision::create(['unique_id' => $this->id] + Arr::except($this->getAttributes(), $excepts));
73
+ $revision = Models\StaffRevision::create(['unique_id' => $this->id] + Arr::except($this->getAttributes(), $excepts));
74
74
 
75
- Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $new->id]);
75
+ $latest = Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $revision->id]);
76
76
 
77
77
  if (!$this->exists) {
78
78
 
79
- $new->unique_id = $new->id;
79
+ $new->unique_id = $latest->id;
80
80
 
81
81
  $new->save();
82
82
 

10

insert id

2017/02/26 12:44

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -74,7 +74,21 @@
74
74
 
75
75
  Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $new->id]);
76
76
 
77
+ if (!$this->exists) {
78
+
79
+ $new->unique_id = $new->id;
80
+
81
+ $new->save();
82
+
83
+ $this->exists = true;
84
+
85
+ $this->wasRecentlyCreated = true;
86
+
87
+ $this->fireModelEvent('created', false);
88
+
77
- $this->finishSave($options);
89
+ $this->finishSave($options);
90
+
91
+ }
78
92
 
79
93
  });
80
94
 

9

comment

2017/02/26 12:43

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
 
40
40
 
41
- 動作未確認ですが,調べながら具体的な実装例を書いてみました。まず,以下のような方法で`staffs`ビューが作られていると仮定します。また,リビジョンは`staff_revisions`,最新リビジョンへのポインタは`staff_latests`が持つとします。
41
+ 動作未確認ですが,調べながら具体的な実装例を書いてみました。まず,以下のような方法で`staffs`ビューが作られていると仮定します。また,リビジョンは`staff_revisions`,最新リビジョンへのポインタは`staff_latests`が持つとします。`staff_revisions.unique_id`,`staff_latests.revision_id`で両者は相互参照し合っています。
42
42
 
43
43
 
44
44
 

8

unique_id

2017/02/26 12:33

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -70,7 +70,7 @@
70
70
 
71
71
  ];
72
72
 
73
- $new = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts));
73
+ $new = Models\StaffRevision::create(['unique_id' => $this->id] + Arr::except($this->getAttributes(), $excepts));
74
74
 
75
75
  Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $new->id]);
76
76
 

7

->id

2017/02/26 12:31

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -70,9 +70,9 @@
70
70
 
71
71
  ];
72
72
 
73
- $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts));
73
+ $new = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts));
74
74
 
75
- Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $id]);
75
+ Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $new->id]);
76
76
 
77
77
  $this->finishSave($options);
78
78
 

6

syntax error

2017/02/26 12:15

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -70,7 +70,7 @@
70
70
 
71
71
  ];
72
72
 
73
- $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts);
73
+ $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts));
74
74
 
75
75
  Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $id]);
76
76
 

5

updateOrC

2017/02/26 12:13

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts);
74
74
 
75
- Models\StaffLatest::where('id', $this->id)->update(['revision_id' => $id]);
75
+ Models\StaffLatest::where('id', $this->id)->updateOrCreate(['revision_id' => $id]);
76
76
 
77
77
  $this->finishSave($options);
78
78
 

4

id

2017/02/26 12:12

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts);
74
74
 
75
- Models\StaffLatest::where('id', 1)->update(['revision_id' => $id]);
75
+ Models\StaffLatest::where('id', $this->id)->update(['revision_id' => $id]);
76
76
 
77
77
  $this->finishSave($options);
78
78
 

3

except

2017/02/26 12:08

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -60,7 +60,17 @@
60
60
 
61
61
  DB::transaction(function () use ($options) {
62
62
 
63
+ $excepts = [
64
+
65
+ $this->getKeyName(),
66
+
67
+ $this->getCreatedAtColumn(),
68
+
69
+ $this->getUpdatedAtColumn(),
70
+
71
+ ];
72
+
63
- $id = Models\StaffRevision::create($this->getFillable());
73
+ $id = Models\StaffRevision::create(Arr::except($this->getAttributes(), $excepts);
64
74
 
65
75
  Models\StaffLatest::where('id', 1)->update(['revision_id' => $id]);
66
76
 

2

追記

2017/02/26 12:05

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -1,4 +1,4 @@
1
- ちょうど自分もLaravelで履歴の管理をやろうとしていたところなので,その方針について回答します。
1
+ ちょうど自分もLaravelでリビジョンの管理をやろうとしていたところなので,その方針について回答します。
2
2
 
3
3
 
4
4
 
@@ -10,15 +10,15 @@
10
10
 
11
11
 
12
12
 
13
- また何を履歴として保存するかですが,(3番を採用する時点で必然的に)**何が変更されようと全部の情報を保存し直す**という解になりますね。BLOB型のカラムが無ければ全然これで問題無いと思います,外部記憶が高価である時代はとっくに終焉しているので,富豪的プログラミングの考え方でいきましょう。
13
+ また何をリビジョンとして保存するかですが,(3番を採用する時点で必然的に)**何が変更されようと全部の情報を保存し直す**という解になりますね。BLOB型のカラムが無ければ全然これで問題無いと思います,外部記憶が高価である時代はとっくに終焉しているので,富豪的プログラミングの考え方でいきましょう。
14
14
 
15
15
 
16
16
 
17
- ---
17
+ ----
18
18
 
19
19
 
20
20
 
21
- << 補足 >>
21
+ << 補足 1 >>
22
22
 
23
23
 
24
24
 
@@ -26,4 +26,52 @@
26
26
 
27
27
 
28
28
 
29
- こういうのはありましたが,全テーブルの履歴が1つに集約されてしまうと扱いにくそうなので,個人的にはあまり使いたく無いですね…
29
+ こういうのはありましたが,全テーブルのリビジョンが1つに集約されてしまうと扱いにくそうなので,個人的にはあまり使いたく無いですね…
30
+
31
+
32
+
33
+ ----
34
+
35
+
36
+
37
+ << 補足 2 (内容は保証しません) >>
38
+
39
+
40
+
41
+ 動作未確認ですが,調べながら具体的な実装例を書いてみました。まず,以下のような方法で`staffs`ビューが作られていると仮定します。また,リビジョンは`staff_revisions`,最新リビジョンへのポインタは`staff_latests`が持つとします。
42
+
43
+
44
+
45
+ - [LaravelのマイグレーションでView Tableを作成する - cakephperの日記(CakePHP, Laravel, PHP)](http://d.hatena.ne.jp/cakephper/20150731/1438347861)
46
+
47
+
48
+
49
+ で,保存処理一式を`Models\Staff::save()`に含めてしまいましょう。ここではべた書きしましたが,トレイトをうまく作るとこの辺りを他のモデルと共通化できるかもしれません。
50
+
51
+
52
+
53
+ ```php
54
+
55
+ public function save(array $options = [])
56
+
57
+ {
58
+
59
+ if ($this->isDirty()) {
60
+
61
+ DB::transaction(function () use ($options) {
62
+
63
+ $id = Models\StaffRevision::create($this->getFillable());
64
+
65
+ Models\StaffLatest::where('id', 1)->update(['revision_id' => $id]);
66
+
67
+ $this->finishSave($options);
68
+
69
+ });
70
+
71
+ }
72
+
73
+ return true;
74
+
75
+ }
76
+
77
+ ```

1

補足

2017/02/26 11:58

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -11,3 +11,19 @@
11
11
 
12
12
 
13
13
  また何を履歴として保存するかですが,(3番を採用する時点で必然的に)**何が変更されようと全部の情報を保存し直す**という解になりますね。BLOB型のカラムが無ければ全然これで問題無いと思います,外部記憶が高価である時代はとっくに終焉しているので,富豪的プログラミングの考え方でいきましょう。
14
+
15
+
16
+
17
+ ---
18
+
19
+
20
+
21
+ << 補足 >>
22
+
23
+
24
+
25
+ [VentureCraft/revisionable: Easily create a revision history for any laravel model](https://github.com/VentureCraft/revisionable)
26
+
27
+
28
+
29
+ こういうのはありましたが,全テーブルの履歴が1つに集約されてしまうと扱いにくそうなので,個人的にはあまり使いたく無いですね…