質問編集履歴

3

画像追加

2018/03/26 15:16

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,14 @@
12
12
 
13
13
 
14
14
 
15
+ ![イメージ説明](3ee4a28e5f1f6c7e1ffeaa6d2a237c15.png)
16
+
17
+
18
+
19
+
20
+
21
+
22
+
15
23
  下記のようなスクリプトで /photos/tag/かもめ
16
24
 
17
25
  とURLを指定し、「かもめ」のタグが付いている画像を表示させたいのですが、

2

インデント修正

2018/03/26 15:16

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -30,11 +30,9 @@
30
30
 
31
31
  ```php
32
32
 
33
-
34
-
35
- public $belongsTo = 'User';
33
+ public $belongsTo = 'User';
36
-
34
+
37
- public $hasAndBelongsToMany = array(
35
+ public $hasAndBelongsToMany = array(
38
36
 
39
37
  'Tdtag' => array(
40
38
 
@@ -42,232 +40,230 @@
42
40
 
43
41
  'order' => 'tag'
44
42
 
43
+           )
44
+
45
+            );
46
+
47
+
48
+
49
+
50
+
51
+ public function findAllByTags($tags = array(), $limit = 50, $page = 1, $criteria = null){
52
+
53
+ if(count($tags) <= 0){
54
+
55
+ return 0;
56
+
57
+              }
58
+
59
+ if(!empty($criteria)) {
60
+
61
+ $criteria = 'AND'.$criteria;
62
+
63
+              }
64
+
65
+
66
+
67
+ $prefix = $this->tablePrefix;
68
+
69
+ $offset = $limit * ($page-1);
70
+
71
+ $posts = $this->query(
72
+
73
+ "SELECT Photo.id,
74
+
75
+ Photo.created,
76
+
77
+ Photo.dir,
78
+
79
+ Photo.user_id,
80
+
81
+ Photo.filename,
82
+
83
+ User.id,
84
+
85
+ User.handlename,
86
+
87
+ COUNT(DISTINCT tdtags.tag) AS uniques
88
+
89
+ FROM
90
+
91
+ {$prefix}photos Photo,
92
+
93
+ {$prefix}photos_tdtags photos_tdtags,
94
+
95
+ {$prefix}tdtags tdtags,
96
+
97
+     {$prefix}users User
98
+
99
+ WHERE Photo.id = photos_tdtags.Photo_id
100
+
101
+ AND User.id = photos.user_id
102
+
103
+ AND tdtags.id = photos_tdtags.tdtag_id
104
+
105
+ AND tdtags.tag IN ('".implode("', '", $tags)."') $criteria
106
+
107
+ GROUP BY photos_tdtags.Photo_id
108
+
109
+ HAVING uniques = '".count($tags)."'
110
+
111
+ ORDER BY Photo.created DESC
112
+
113
+ LIMIT $offset, $limit"
114
+
115
+ );
116
+
117
+
118
+
119
+ return $posts;
120
+
121
+ }
122
+
123
+ ```
124
+
125
+
126
+
127
+ PhotosController.php (タグから画像を引っ張ってくるページを表示するためのスクリプト)
128
+
129
+
130
+
131
+ ```php
132
+
133
+ public function tag($tag = null) {
134
+
135
+ $tags = array();
136
+
137
+ App::uses('Sanitize','Utility');
138
+
139
+ $this->Sanitize = new Sanitize;
140
+
141
+
142
+
143
+ if(isset($this->params['pass'])){
144
+
145
+ $i=0;
146
+
147
+ $title="タグ:";
148
+
149
+ foreach($this->params['pass'] as $tag):
150
+
151
+ $this->Sanitize->paranoid($tag, array(' '));
152
+
153
+ $tags[] = $tag;
154
+
155
+ if($i==0){
156
+
157
+ $title=$title.$tag;
158
+
159
+ }else{
160
+
161
+ $title=$title.",".$tag;
162
+
163
+ }
164
+
165
+ $i++;
166
+
167
+ endforeach;
168
+
169
+
170
+
171
+                      }  
172
+
173
+ $paging['url'] = '/photos/tag'. implode('/', $tags);
174
+
175
+
176
+
177
+ $params = array(
178
+
179
+ 'conditions' => array(
180
+
181
+ 'Tdtag.tag' => $tags,
182
+
183
+ )
184
+
185
+ );
186
+
187
+
188
+
189
+ //paging['total'] = $this->Photo->find('count', $params);
190
+
191
+ $paging['total'] = $this->Photo->findCountByTags($tags);
192
+
193
+ //debug($paging['total']);
194
+
195
+ if($paging['total'] > 0){
196
+
197
+ //$posts = $this->Photo->findAllByTags($tags);
198
+
199
+ $this->set('photo',$this->Photo->findAllByTags($tags));
200
+
201
+ debug($this->Photo->findAllByTags($tags)); ★ここのデバッグ結果を下に記載しています
202
+
203
+ $this->set('company',$title);
204
+
205
+ $this->render('/Companys/company');
206
+
207
+
208
+
209
+              }else {
210
+
211
+ //タグの記事が無い場合の処理。記事が見つかりませんでしたみたいな。
212
+
213
+ //exit;
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+ 帰ってきたデバッグ結果
222
+
223
+
224
+
225
+ array(
226
+
227
+ (int) 0 => array(
228
+
229
+ 'Photo' => array(
230
+
231
+ 'id' => '14065',
232
+
233
+ 'created' => '2018-01-16 20:19:26',
234
+
235
+ 'dir' => '/files/aaa/',
236
+
237
+ 'user_id' => '1',
238
+
239
+ 'filename' => '/xxx.jpg',
240
+
241
+ ),
242
+
243
+ 'User' => array(
244
+
245
+       'id' => '538', 
246
+
247
+ 'handlename' => '前原誠司', 
248
+
249
+ 'url' => ''
250
+
251
+ ),
252
+
253
+ (int) 0 => array(
254
+
255
+ 'uniques' => '1'
256
+
45
257
  )
46
258
 
47
- );
259
+ )
48
-
49
-
50
-
51
-
52
-
53
- public function findAllByTags($tags = array(), $limit = 50, $page = 1, $criteria = null){
260
+
54
-
55
- if(count($tags) <= 0){
56
-
57
- return 0;
58
-
59
- }
60
-
61
- if(!empty($criteria)) {
62
-
63
- $criteria = 'AND'.$criteria;
64
-
65
- }
66
-
67
-
68
-
69
- $prefix = $this->tablePrefix;
70
-
71
- $offset = $limit * ($page-1);
72
-
73
- $posts = $this->query(
74
-
75
- "SELECT Photo.id,
76
-
77
- Photo.created,
78
-
79
- Photo.dir,
80
-
81
- Photo.user_id,
82
-
83
- Photo.filename,
84
-
85
- User.id,
86
-
87
- User.handlename,
88
-
89
- COUNT(DISTINCT tdtags.tag) AS uniques
90
-
91
- FROM
92
-
93
- {$prefix}photos Photo,
94
-
95
- {$prefix}photos_tdtags photos_tdtags,
96
-
97
- {$prefix}tdtags tdtags,
98
-
99
-           {$prefix}users User
100
-
101
- WHERE Photo.id = photos_tdtags.Photo_id
102
-
103
- AND User.id = photos.user_id
104
-
105
- AND tdtags.id = photos_tdtags.tdtag_id
106
-
107
- AND tdtags.tag IN ('".implode("', '", $tags)."') $criteria
108
-
109
- GROUP BY photos_tdtags.Photo_id
110
-
111
- HAVING uniques = '".count($tags)."'
112
-
113
- ORDER BY Photo.created DESC
114
-
115
- LIMIT $offset, $limit"
116
-
117
- );
261
+ )
118
-
119
-
120
-
262
+
263
+
264
+
121
- return $posts;
265
+ UserテーブルのID=1は、安倍晋三にも関わらず、帰ってくるユーザーデータはUserテーブルからランダムなものが帰ってきてしまっています。
122
-
123
- }
266
+
267
+
124
268
 
125
269
  ```
126
-
127
-
128
-
129
- PhotosController.php (タグから画像を引っ張ってくるページを表示するためのスクリプト)
130
-
131
-
132
-
133
- ```php
134
-
135
- public function tag($tag = null) {
136
-
137
- $tags = array();
138
-
139
- App::uses('Sanitize','Utility');
140
-
141
- $this->Sanitize = new Sanitize;
142
-
143
-
144
-
145
- if(isset($this->params['pass'])){
146
-
147
- $i=0;
148
-
149
- $title="タグ:";
150
-
151
- foreach($this->params['pass'] as $tag):
152
-
153
- $this->Sanitize->paranoid($tag, array(' '));
154
-
155
- $tags[] = $tag;
156
-
157
- if($i==0){
158
-
159
- $title=$title.$tag;
160
-
161
- }else{
162
-
163
- $title=$title.",".$tag;
164
-
165
- }
166
-
167
- $i++;
168
-
169
- endforeach;
170
-
171
-
172
-
173
- }
174
-
175
- $paging['url'] = '/photos/tag'. implode('/', $tags);
176
-
177
-
178
-
179
- $params = array(
180
-
181
- 'conditions' => array(
182
-
183
- 'Tdtag.tag' => $tags,
184
-
185
- )
186
-
187
- );
188
-
189
-
190
-
191
- //paging['total'] = $this->Photo->find('count', $params);
192
-
193
- $paging['total'] = $this->Photo->findCountByTags($tags);
194
-
195
- //debug($paging['total']);
196
-
197
- if($paging['total'] > 0){
198
-
199
- //$posts = $this->Photo->findAllByTags($tags);
200
-
201
- $this->set('photo',$this->Photo->findAllByTags($tags));
202
-
203
- debug($this->Photo->findAllByTags($tags)); ★ここのデバッグ結果を下に記載しています
204
-
205
- $this->set('company',$title);
206
-
207
- $this->render('/Companys/company');
208
-
209
-
210
-
211
- }
212
-
213
- else {
214
-
215
- //タグの記事が無い場合の処理。記事が見つかりませんでしたみたいな。
216
-
217
- //exit;
218
-
219
- }
220
-
221
- }
222
-
223
-
224
-
225
- 帰ってきたデバッグ結果
226
-
227
-
228
-
229
- array(
230
-
231
- (int) 0 => array(
232
-
233
- 'Photo' => array(
234
-
235
- 'id' => '14065',
236
-
237
- 'created' => '2018-01-16 20:19:26',
238
-
239
- 'dir' => '/files/aaa/',
240
-
241
- 'user_id' => '1',
242
-
243
- 'filename' => '/xxx.jpg',
244
-
245
- ),
246
-
247
- 'User' => array(
248
-
249
-               'id' => '538', 
250
-
251
- 'handlename' => '前原誠司', 
252
-
253
- 'url' => ''
254
-
255
- ),
256
-
257
- (int) 0 => array(
258
-
259
- 'uniques' => '1'
260
-
261
- )
262
-
263
- )
264
-
265
- )
266
-
267
-
268
-
269
- UserテーブルのID=1は、安倍晋三にも関わらず、帰ってくるユーザーデータはUserテーブルからランダムなものが帰ってきてしまっています。
270
-
271
-
272
-
273
- ```

1

修正

2018/03/25 05:12

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -86,8 +86,6 @@
86
86
 
87
87
  User.handlename,
88
88
 
89
- User.url,
90
-
91
89
  COUNT(DISTINCT tdtags.tag) AS uniques
92
90
 
93
91
  FROM
@@ -202,7 +200,7 @@
202
200
 
203
201
  $this->set('photo',$this->Photo->findAllByTags($tags));
204
202
 
205
- debug($this->Photo->findAllByTags($tags));
203
+ debug($this->Photo->findAllByTags($tags)); ★ここのデバッグ結果を下に記載しています
206
204
 
207
205
  $this->set('company',$title);
208
206
 
@@ -224,4 +222,52 @@
224
222
 
225
223
 
226
224
 
225
+ 帰ってきたデバッグ結果
226
+
227
+
228
+
229
+ array(
230
+
231
+ (int) 0 => array(
232
+
233
+ 'Photo' => array(
234
+
235
+ 'id' => '14065',
236
+
237
+ 'created' => '2018-01-16 20:19:26',
238
+
239
+ 'dir' => '/files/aaa/',
240
+
241
+ 'user_id' => '1',
242
+
243
+ 'filename' => '/xxx.jpg',
244
+
245
+ ),
246
+
247
+ 'User' => array(
248
+
249
+               'id' => '538', 
250
+
251
+ 'handlename' => '前原誠司', 
252
+
253
+ 'url' => ''
254
+
255
+ ),
256
+
257
+ (int) 0 => array(
258
+
259
+ 'uniques' => '1'
260
+
261
+ )
262
+
263
+ )
264
+
265
+ )
266
+
267
+
268
+
269
+ UserテーブルのID=1は、安倍晋三にも関わらず、帰ってくるユーザーデータはUserテーブルからランダムなものが帰ってきてしまっています。
270
+
271
+
272
+
227
273
  ```