質問編集履歴

6

a

2018/11/22 10:43

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -522,7 +522,7 @@
522
522
 
523
523
  $this->response->type("json");
524
524
 
525
- $response = json_encode($this->paginate($query));
525
+ $response = ($this->paginate($query));
526
526
 
527
527
  $this->set("response", $response);
528
528
 

5

2018/11/22 10:43

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -458,6 +458,90 @@
458
458
 
459
459
 
460
460
 
461
+ - popobotさんにご回答頂いた内容を参考に、CommentsControllerを編集した -> Missing Templateエラーが発生した
462
+
463
+ `Controller/CommentsController.php`(コメント取得部分等を抜粋、コメント追加アクション等一部は省略)
464
+
465
+ ```php
466
+
467
+ <?php
468
+
469
+ namespace App\Controller;
470
+
471
+
472
+
473
+ class CommentsController extends AppController
474
+
475
+ {
476
+
477
+ public $paginate = [
478
+
479
+ 'limit' => 5,
480
+
481
+ 'maxLimit' => 10
482
+
483
+ ];
484
+
485
+
486
+
487
+ public function __construct($request, $response)
488
+
489
+ {
490
+
491
+ parent::__construct($request, $response);
492
+
493
+ }
494
+
495
+
496
+
497
+ public function initialize()
498
+
499
+ {
500
+
501
+ parent::initialize();
502
+
503
+ $this->loadComponent('Security');
504
+
505
+ $this->loadComponent('Paginator');
506
+
507
+ $this->loadComponent('RequestHandler');
508
+
509
+ }
510
+
511
+
512
+
513
+ public function get()
514
+
515
+ {
516
+
517
+ $id = $this->request->getQuery("article_id");
518
+
519
+ $query = $this->Comments->find()->where(["article_id" => $id]);
520
+
521
+ $this->response->charset("UTF-8");
522
+
523
+ $this->response->type("json");
524
+
525
+ $response = json_encode($this->paginate($query));
526
+
527
+ $this->set("response", $response);
528
+
529
+ $this->set("_serialize", ["response"]);
530
+
531
+ }
532
+
533
+ }
534
+
535
+ ```
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
461
545
  何かエラーに心当たりのある方がいらっしゃいましたら、教えていただけますと幸いです
462
546
 
463
547
  よろしくお願いいたします

4

2018/11/22 08:55

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -34,16 +34,22 @@
34
34
 
35
35
 
36
36
 
37
- 現在コードが入ったUSBを外出先に忘れてきてしまっています。
37
+ ~~現在コードが入ったUSBを外出先に忘れてきてしまっています。
38
-
38
+
39
- USBを回収した後に追記いたします。
39
+ USBを回収した後に追記いたします。~~
40
-
41
-
42
-
40
+
41
+
42
+
43
- 動作確認に必要そうなコントローラ、ビューファイルを追記いたしました。
43
+ **動作確認に必要そうなコントローラ、ビューファイルを追記いたしました。**
44
+
45
+
44
46
 
45
47
  不足な点があれば、修正依頼を頂けると幸いです。
46
48
 
49
+ 下のソースコードでは`echo json_encode($this->paginate($query))`を使い、サーバから結果を返すようになっていますが、その部分を`set _serialize`を用いた手法に変えた際にうまく動かなくなってしまいます。
50
+
51
+
52
+
47
53
  `src/Controller/ArticlesController.php`(viewアクション、paginatorで使う部分を抜粋)
48
54
 
49
55
  ```php
@@ -268,8 +274,6 @@
268
274
 
269
275
  {
270
276
 
271
- $this->layout = "ajax";//??
272
-
273
277
  $id = $this->request->getQuery("article_id");
274
278
 
275
279
  $query = $this->Comments->find()->where(["article_id" => $id]);

3

a

2018/11/21 09:29

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,408 @@
40
40
 
41
41
 
42
42
 
43
+ 動作確認に必要そうなコントローラ、ビューファイルを追記いたしました。
44
+
45
+ 不足な点があれば、修正依頼を頂けると幸いです。
46
+
47
+ `src/Controller/ArticlesController.php`(viewアクション、paginatorで使う部分を抜粋)
48
+
49
+ ```php
50
+
51
+ public $paginate = [
52
+
53
+ 'limit' => 5,
54
+
55
+ 'maxLimit' => 10
56
+
57
+ ];
58
+
59
+
60
+
61
+ public function initialize()
62
+
63
+ {
64
+
65
+ parent::initialize();
66
+
67
+ $this->loadComponent('Security');
68
+
69
+ $this->loadComponent('Paginator');
70
+
71
+ }
72
+
73
+
74
+
75
+ public function view($id = null)
76
+
77
+ {
78
+
79
+ $article = $this->Articles->get($id);
80
+
81
+ $comment = $this->Articles->Comments->newEntity();
82
+
83
+ $query = $this->Articles->Comments->find()->where(["article_id" => $id]);
84
+
85
+ $this->set(compact("article", "comment"));
86
+
87
+ $this->set("comments", $this->paginate($query));
88
+
89
+ }
90
+
91
+ ```
92
+
93
+
94
+
95
+ `src/Template/Articles/view.ctp`
96
+
97
+ ```php
98
+
99
+ <h1><?= h($article->title) ?></h1>
100
+
101
+ <p><?= h($article->name) ?></p>
102
+
103
+ <p><?= h($article->body) ?></p>
104
+
105
+ <p><small>Created: <?= $article->created->format(DATE_RFC850) ?></small></p>
106
+
107
+ <hr>
108
+
109
+
110
+
111
+ <?php
112
+
113
+ echo $this->Form->create($comment, ["url" => ["controller" => "Comments", "action" => "add"]]);
114
+
115
+ echo $this->Form->control('title');
116
+
117
+ echo $this->Form->control('body', ['rows' => '3']);
118
+
119
+ echo $this->Form->hidden("article_id", ["default"=>h($article->id)]);
120
+
121
+ echo $this->Form->button(__('Save Article'));
122
+
123
+ echo $this->Form->end();
124
+
125
+ ?>
126
+
127
+ <?php if (!$comments->isEmpty()): ?>
128
+
129
+
130
+
131
+ <?php foreach ($comments as $comment): ?>
132
+
133
+ <?php var_dump($comment) ?>
134
+
135
+ <?php endforeach; ?>
136
+
137
+ <?php $this->Paginator->options([
138
+
139
+ 'url' => [
140
+
141
+ 'controller' => 'Comments',
142
+
143
+ 'action' => 'get'
144
+
145
+ ]
146
+
147
+ ]); ?>
148
+
149
+ <ul class="pagination">
150
+
151
+ <?= $this->Paginator->first('<<') ?>
152
+
153
+ <?= $this->Paginator->prev('<') ?>
154
+
155
+ <?php if (!empty($this->Paginator->numbers())): ?>
156
+
157
+ <?= $this->Paginator->numbers() ?>
158
+
159
+ <?php else: ?>
160
+
161
+ <li><?= $this->Paginator->current("Comments") ?></li>
162
+
163
+ <?php endif; ?>
164
+
165
+ <?= $this->Paginator->next('>') ?>
166
+
167
+ <?= $this->Paginator->last('>>') ?>
168
+
169
+ </ul>
170
+
171
+ <?php else: ?>
172
+
173
+ <p>この記事にはまだコメントがありません。</p>
174
+
175
+ <?php endif; ?>
176
+
177
+
178
+
179
+ <hr>
180
+
181
+
182
+
183
+ <div id="result">
184
+
185
+ </div>
186
+
187
+
188
+
189
+
190
+
191
+ <script>
192
+
193
+ $(function () {
194
+
195
+ $(".pagination a").on("click", function () {
196
+
197
+ $.getJSON($(this).attr("href"), { article_id: <?= $article->id ?> }, function (data){
198
+
199
+ console.log(data);
200
+
201
+ });
202
+
203
+ return false;
204
+
205
+ });
206
+
207
+ });
208
+
209
+ </script>
210
+
211
+ ```
212
+
213
+ `Controller/CommentsController.php`(コメント取得部分等を抜粋、コメント追加アクション等一部は省略)
214
+
215
+ ```php
216
+
217
+ <?php
218
+
219
+ namespace App\Controller;
220
+
221
+
222
+
223
+ class CommentsController extends AppController
224
+
225
+ {
226
+
227
+ public $paginate = [
228
+
229
+ 'limit' => 5,
230
+
231
+ 'maxLimit' => 10
232
+
233
+ ];
234
+
235
+
236
+
237
+ public function __construct($request, $response)
238
+
239
+ {
240
+
241
+ parent::__construct($request, $response);
242
+
243
+ $this->autoRender = false;
244
+
245
+ }
246
+
247
+
248
+
249
+ public function initialize()
250
+
251
+ {
252
+
253
+ parent::initialize();
254
+
255
+ $this->loadComponent('Security');
256
+
257
+ $this->loadComponent('Paginator');
258
+
259
+ $this->loadComponent('RequestHandler');
260
+
261
+ }
262
+
263
+
264
+
265
+
266
+
267
+ public function get()
268
+
269
+ {
270
+
271
+ $this->layout = "ajax";//??
272
+
273
+ $id = $this->request->getQuery("article_id");
274
+
275
+ $query = $this->Comments->find()->where(["article_id" => $id]);
276
+
277
+ $this->response->charset("UTF-8");
278
+
279
+ $this->response->type("json");
280
+
281
+ echo json_encode($this->paginate($query));
282
+
283
+ }
284
+
285
+ }
286
+
287
+ ```
288
+
289
+
290
+
291
+ `Model/Table/ArticlesTable.php`
292
+
293
+ ```php
294
+
295
+ <?php
296
+
297
+ namespace App\Model\Table;
298
+
299
+
300
+
301
+ use Cake\ORM\Table;
302
+
303
+ use Cake\Validation\Validator;
304
+
305
+
306
+
307
+ class ArticlesTable extends Table
308
+
309
+ {
310
+
311
+ public function initialize(array $config)
312
+
313
+ {
314
+
315
+ $this->addBehavior("Timestamp");
316
+
317
+ $this->hasMany("Comments")
318
+
319
+ ->setDependent(true);
320
+
321
+ }
322
+
323
+
324
+
325
+ public function validationDefault(Validator $validator)
326
+
327
+ {
328
+
329
+ $validator
330
+
331
+ ->notEmpty("title")
332
+
333
+ ->requirePresence("title")
334
+
335
+ ->notEmpty("name")
336
+
337
+ ->requirePresence("name")
338
+
339
+ ->notEmpty("body")
340
+
341
+ ->maxLength("body", 140, "140")
342
+
343
+ ->requirePresence("body");
344
+
345
+
346
+
347
+ return $validator;
348
+
349
+ }
350
+
351
+ }
352
+
353
+ ```
354
+
355
+
356
+
357
+ `src/Model/Table/CommentsTable.php`
358
+
359
+ ```php
360
+
361
+ <?php
362
+
363
+ namespace App\Model\Table;
364
+
365
+
366
+
367
+ use Cake\ORM\Table;
368
+
369
+ use Cake\Validation\Validator;
370
+
371
+
372
+
373
+ class CommentsTable extends Table
374
+
375
+ {
376
+
377
+ public function initialize(array $config)
378
+
379
+ {
380
+
381
+ $this->addBehavior('Timestamp');
382
+
383
+ $this->belongsTo("Articles");
384
+
385
+ }
386
+
387
+
388
+
389
+ public function validationDefault(Validator $validator)
390
+
391
+ {
392
+
393
+ $validator
394
+
395
+ ->notEmpty('title')
396
+
397
+ ->requirePresence('title')
398
+
399
+ ->notEmpty('body')
400
+
401
+ ->requirePresence('body');
402
+
403
+
404
+
405
+ return $validator;
406
+
407
+ }
408
+
409
+ }
410
+
411
+ ```
412
+
413
+
414
+
415
+ Commentsテーブルの構造も記載いたします。
416
+
417
+ `article_id`に外部キーを用いてコメントの属する記事を参照するようにしています。
418
+
419
+ ```sql
420
+
421
+ CREATE TABLE `comments` (
422
+
423
+ `id` int(11) NOT NULL AUTO_INCREMENT,
424
+
425
+ `article_id` int(10) unsigned NOT NULL,
426
+
427
+ `title` varchar(255) NOT NULL,
428
+
429
+ `body` text,
430
+
431
+ `created` datetime DEFAULT NULL,
432
+
433
+ PRIMARY KEY (`id`),
434
+
435
+ KEY `article_key` (`article_id`),
436
+
437
+ CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`)
438
+
439
+ ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4
440
+
441
+ ```
442
+
443
+
444
+
43
445
  ### 試したこと
44
446
 
45
447
 

2

2018/11/21 09:27

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- CakePHPを使い、Ajax通信でデータベースからデーターを取得するソースを作成しています
5
+ CakePHP3を使い、Ajax通信でデータベースからデーターを取得するソースを作成しています
6
6
 
7
7
 
8
8
 

1

2018/11/20 10:02

投稿

nnafa
nnafa

スコア12

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- - `echo json_encode($results);` -> 無事表示できた。開発者ツールでのエラーもなし
47
+ - `echo json_encode($res);` -> 無事表示できた。開発者ツールでのエラーもなし
48
48
 
49
49
 
50
50