質問編集履歴

1

補足追記

2015/12/15 07:39

投稿

otagroove
otagroove

スコア28

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,235 @@
21
21
  お手数おかけいたしますが、ご教示いただければ幸いです。
22
22
 
23
23
  よろしくお願い致します。
24
+
25
+
26
+
27
+
28
+
29
+ ###補足
30
+
31
+
32
+
33
+ **1.開発環境**
34
+
35
+ - eclipse4.5.1
36
+
37
+ - CakePHP3.1.5
38
+
39
+
40
+
41
+ **2.モデル情報**
42
+
43
+
44
+
45
+ Modelは自動生成したモデル
46
+
47
+ 使用コマンド:bin\cake bake model persons
48
+
49
+ なお、生成されてソースは下記に添付いたします。
50
+
51
+
52
+
53
+ 今回コードを補完したいのはPersonsTableクラスに入っている「newEntity」になります。
54
+
55
+ なお、コントローラークラスでは下記のように呼び出しております。
56
+
57
+ ```PHP
58
+
59
+ class PersonsController extends AppController {
60
+
61
+ public function add() {
62
+
63
+ $person = $this->Persons->newEntity ();
64
+
65
+ }
66
+
67
+ }
68
+
69
+ ```
70
+
71
+
72
+
73
+ このCakePHPでインスタンス化されたPersonsTableクラスの「newEntity」というメソッドのコードを保管したいです。
74
+
75
+
76
+
77
+ お手数おかけいたしますが、何卒よろしくお願い致します。
78
+
79
+
80
+
81
+ ・生成されたテーブルクラス
82
+
83
+ ```PHP
84
+
85
+ <?php
86
+
87
+
88
+
89
+ namespace App\Model\Table;
90
+
91
+
92
+
93
+ use App\Model\Entity\Person;
94
+
95
+ use Cake\ORM\Query;
96
+
97
+ use Cake\ORM\RulesChecker;
98
+
99
+ use Cake\ORM\Table;
100
+
101
+ use Cake\Validation\Validator;
102
+
103
+
104
+
105
+ /**
106
+
107
+ * Persons Model
108
+
109
+ */
110
+
111
+ class PersonsTable extends Table {
112
+
113
+
114
+
115
+ /**
116
+
117
+ * Initialize method
118
+
119
+ *
120
+
121
+ * @param array $config
122
+
123
+ * The configuration for the Table.
124
+
125
+ * @return void
126
+
127
+ */
128
+
129
+ public function initialize(array $config) {
130
+
131
+ parent::initialize ($config);
132
+
133
+
134
+
135
+ $this->table ('persons');
136
+
137
+ $this->displayField ('name');
138
+
139
+ $this->primaryKey ('id');
140
+
141
+ }
142
+
143
+
144
+
145
+ /**
146
+
147
+ * Default validation rules.
148
+
149
+ *
150
+
151
+ * @param \Cake\Validation\Validator $validator
152
+
153
+ * Validator instance.
154
+
155
+ * @return \Cake\Validation\Validator
156
+
157
+ */
158
+
159
+ public function validationDefault(Validator $validator) {
160
+
161
+ $validator->add ('id', 'valid', [
162
+
163
+ 'rule' => 'numeric' ])->allowEmpty ('id', 'create');
164
+
165
+
166
+
167
+ $validator->requirePresence ('name', 'create')->notEmpty ('name');
168
+
169
+
170
+
171
+ $validator->add ('age', 'valid', [
172
+
173
+ 'rule' => 'numeric' ])->allowEmpty ('age');
174
+
175
+
176
+
177
+ $validator->allowEmpty ('mail');
178
+
179
+
180
+
181
+ return $validator;
182
+
183
+ }
184
+
185
+ }
186
+
187
+ ```
188
+
189
+
190
+
191
+ ・生成されたEntitiyクラス
192
+
193
+ ```PHP
194
+
195
+ <?php
196
+
197
+
198
+
199
+ namespace App\Model\Entity;
200
+
201
+
202
+
203
+ use Cake\ORM\Entity;
204
+
205
+
206
+
207
+ /**
208
+
209
+ * Person Entity.
210
+
211
+ *
212
+
213
+ * @property int $id
214
+
215
+ * @property string $name
216
+
217
+ * @property int $age
218
+
219
+ * @property string $mail
220
+
221
+ */
222
+
223
+ class Person extends Entity {
224
+
225
+
226
+
227
+ /**
228
+
229
+ * Fields that can be mass assigned using newEntity() or patchEntity().
230
+
231
+ *
232
+
233
+ * Note that when '*' is set to true, this allows all unspecified fields to
234
+
235
+ * be mass assigned. For security purposes, it is advised to set '*' to false
236
+
237
+ * (or remove it), and explicitly make individual fields accessible as needed.
238
+
239
+ *
240
+
241
+ * @var array
242
+
243
+ */
244
+
245
+ protected $_accessible = [
246
+
247
+ '*' => true,
248
+
249
+ 'id' => false ];
250
+
251
+ }
252
+
253
+
254
+
255
+ ```