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

質問編集履歴

1

内容を訂正

2016/01/30 05:29

投稿

atsupoooon
atsupoooon

スコア47

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,7 @@
2
2
  php 5.6
3
3
 
4
4
  投稿した個別の記事ないには、コメント件数を表示できましたが、
5
- 一覧ページにも表示したいのですが、
5
+ 一覧ページにも表示したいのですが、できません。
6
- できません。
7
6
 
8
7
  find('count')で表示させようとしております。
9
8
 
@@ -17,24 +16,49 @@
17
16
 
18
17
  というイメージなのですが、このイメージで正しいのでしょうか?
19
18
 
19
+ この上記のイメージが間違っていました。
20
20
 
21
+
22
+ 自分なりに考え直した結果。これが正しい処理の方法かと思っています。
21
- ```ここに言語を入力
23
+ 1、controller
22
- public function index() {
24
+ ・virtualFieldsを追加。
25
+ ・find('all')で指定のfieldsの取得して、countで合計を出す。
26
+ ・それをpostのidでグループ化
27
+ ・combineで配列を組み直す
28
+ ・最後にviewに渡す。
29
+
30
+ しかし、viewに渡してからの表示方法がわかりません。。。
31
+
32
+ コントローラーからのviewに返ってきた値は、
33
+ 下記になります。
34
+ array(
23
- $this->Post->recursive = 0;
35
+ '' => '0',
24
- $this->set('posts', $this->Paginator->paginate());
36
+ (int) 2 => '16',
25
-
37
+ (int) 4 => '5',
38
+ (int) 7 => '15',
39
+ (int) 8 => '7',
40
+ (int) 9 => '1',
41
+ (int) 13 => '3',
42
+ (int) 17 => '3',
43
+ (int) 18 => '1',
44
+ (int) 19 => '1',
45
+ (int) 28 => '3',
46
+ (int) 33 => '3'
47
+ )
48
+
49
+
50
+ コントローラーがこちらです。
51
+ ```php
26
- //コメント件数表示
52
+ //コメント件数表示
53
+ $this->Post->virtualFields['num'] = 0;
27
- $data = $this->Post->find('count', array(
54
+ $data = $this->Post->Comment->find('all', array(
28
- 'fields' => array('Post.id','Comment.Post_id'),
55
+ 'fields'=> array('Post.id', 'Comment.cosme_id', 'count(Post.id) as Post__num'),
29
- 'conditions' => array(
30
- 'Post.id' => 'Comment.Post_id',
56
+ 'group' => array('Post.id')
31
- ),
32
- ));
57
+ ));
58
+ $results_tmp = Hash::combine($data, '{n}.Post.id', '{n}.Post.num');
33
- $this->set(compact('data', $data));
59
+ $this->set(compact('results_tmp'));
34
- }
35
60
  ```
36
61
 
37
- 上記の内容で(int) 0と返ってきます。。。
38
62
 
39
- ご指摘して頂けますしょうか?
63
+ 最後のviewの表示ができません。
40
64
  宜しくお願い致します。