質問編集履歴

1

内容を訂正

2016/01/30 05:29

投稿

atsupoooon
atsupoooon

スコア47

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,7 @@
6
6
 
7
7
  投稿した個別の記事ないには、コメント件数を表示できましたが、
8
8
 
9
- 一覧ページにも表示したいのですが、
9
+ 一覧ページにも表示したいのですが、できません。
10
-
11
- できません。
12
10
 
13
11
 
14
12
 
@@ -36,44 +34,94 @@
36
34
 
37
35
 
38
36
 
37
+ この上記のイメージが間違っていました。
39
38
 
40
39
 
41
- ```ここに言語を入力
42
40
 
43
- public function index() {
44
41
 
45
- $this->Post->recursive = 0;
46
42
 
47
- $this->set('posts', $this->Paginator->paginate());
43
+ 自分なりに考え直した結果。これが正しい処理の方法かと思っています。
48
44
 
49
-
45
+ 1、controller
50
46
 
51
- //コメント件数表示
47
+ ・virtualFieldsを追加。
52
48
 
53
- $data = $this->Post->find('count', array(
49
+ find('all')で指定のfieldsの取得して、countで合計を出す。
54
50
 
55
- 'fields' => array('Post.id','Comment.Post_id'),
51
+ ・それをpostidでグループ化
56
52
 
57
- 'conditions' => array(
53
+ combineで配列を組み直す
58
54
 
59
- 'Post.id' => 'Comment.Post_id',
55
+ ・最後にviewに渡す。
60
56
 
61
- ),
62
57
 
63
- ));
64
58
 
65
- $this->set(compact('data', $data));
59
+ しかし、viewに渡してからの表示方法がわかりません。。。
66
60
 
61
+
62
+
63
+ コントローラーからのviewに返ってきた値は、
64
+
65
+ 下記になります。
66
+
67
+ array(
68
+
69
+ '' => '0',
70
+
71
+ (int) 2 => '16',
72
+
73
+ (int) 4 => '5',
74
+
75
+ (int) 7 => '15',
76
+
77
+ (int) 8 => '7',
78
+
79
+ (int) 9 => '1',
80
+
81
+ (int) 13 => '3',
82
+
83
+ (int) 17 => '3',
84
+
85
+ (int) 18 => '1',
86
+
87
+ (int) 19 => '1',
88
+
89
+ (int) 28 => '3',
90
+
91
+ (int) 33 => '3'
92
+
67
- }
93
+ )
94
+
95
+
96
+
97
+
98
+
99
+ コントローラーがこちらです。
100
+
101
+ ```php
102
+
103
+ //コメント件数表示
104
+
105
+ $this->Post->virtualFields['num'] = 0;
106
+
107
+ $data = $this->Post->Comment->find('all', array(
108
+
109
+ 'fields'=> array('Post.id', 'Comment.cosme_id', 'count(Post.id) as Post__num'),
110
+
111
+ 'group' => array('Post.id')
112
+
113
+ ));
114
+
115
+ $results_tmp = Hash::combine($data, '{n}.Post.id', '{n}.Post.num');
116
+
117
+ $this->set(compact('results_tmp'));
68
118
 
69
119
  ```
70
120
 
71
121
 
72
122
 
73
- 上記の内容で(int) 0と返ってきます。。。
74
123
 
75
124
 
76
-
77
- ご指摘して頂けすでしょうか?
125
+ 最後のviewので表示ができせん。
78
126
 
79
127
  宜しくお願い致します。