CakePHP2.9.1でhasAndBelongsToManyを活用したタグ機能を作成しています。
タグが付いている画像を表示するためのスクリプトを書いています。
・photosテーブル・・・画像のデータが入っている
・belongstoでuserテーブルをアソシエーションしており、投稿者データはuserテーブルに入っている
・Tdtagテーブル・・・タグのテーブル
・photos_tdtagsテーブル・・・アソシエーションのためのテーブル
下記のようなスクリプトで /photos/tag/かもめ
とURLを指定し、「かもめ」のタグが付いている画像を表示させたいのですが、
userテーブルをアソシエーションして持ってくるユーザーデータがランダムなデータになっており、
きちんとphotoテーブルのuser_idカラムに入っているuserデータを持ってきてくれません。
どうすれば上手くいくでしょうか...
Photo.php(モデル)
php
1public $belongsTo = 'User'; 2public $hasAndBelongsToMany = array( 3 'Tdtag' => array( 4 'className' => 'Tdtag', 5 'order' => 'tag' 6 ) 7 ); 8 9 10public function findAllByTags($tags = array(), $limit = 50, $page = 1, $criteria = null){ 11 if(count($tags) <= 0){ 12 return 0; 13 } 14 if(!empty($criteria)) { 15 $criteria = 'AND'.$criteria; 16 } 17 18 $prefix = $this->tablePrefix; 19 $offset = $limit * ($page-1); 20 $posts = $this->query( 21 "SELECT Photo.id, 22 Photo.created, 23 Photo.dir, 24 Photo.user_id, 25 Photo.filename, 26 User.id, 27 User.handlename, 28 COUNT(DISTINCT tdtags.tag) AS uniques 29 FROM 30 {$prefix}photos Photo, 31 {$prefix}photos_tdtags photos_tdtags, 32 {$prefix}tdtags tdtags, 33 {$prefix}users User 34 WHERE Photo.id = photos_tdtags.Photo_id 35 AND User.id = photos.user_id 36 AND tdtags.id = photos_tdtags.tdtag_id 37 AND tdtags.tag IN ('".implode("', '", $tags)."') $criteria 38 GROUP BY photos_tdtags.Photo_id 39 HAVING uniques = '".count($tags)."' 40 ORDER BY Photo.created DESC 41 LIMIT $offset, $limit" 42 ); 43 44 return $posts; 45 }
PhotosController.php (タグから画像を引っ張ってくるページを表示するためのスクリプト)
php
1public function tag($tag = null) { 2 $tags = array(); 3 App::uses('Sanitize','Utility'); 4 $this->Sanitize = new Sanitize; 5 6 if(isset($this->params['pass'])){ 7 $i=0; 8 $title="タグ:"; 9 foreach($this->params['pass'] as $tag): 10 $this->Sanitize->paranoid($tag, array(' ')); 11 $tags[] = $tag; 12 if($i==0){ 13 $title=$title.$tag; 14 }else{ 15 $title=$title.",".$tag; 16 } 17 $i++; 18 endforeach; 19 20 } 21 $paging['url'] = '/photos/tag'. implode('/', $tags); 22 23 $params = array( 24 'conditions' => array( 25 'Tdtag.tag' => $tags, 26 ) 27 ); 28 29 //paging['total'] = $this->Photo->find('count', $params); 30 $paging['total'] = $this->Photo->findCountByTags($tags); 31 //debug($paging['total']); 32 if($paging['total'] > 0){ 33 //$posts = $this->Photo->findAllByTags($tags); 34 $this->set('photo',$this->Photo->findAllByTags($tags)); 35 debug($this->Photo->findAllByTags($tags)); ★ここのデバッグ結果を下に記載しています 36 $this->set('company',$title); 37 $this->render('/Companys/company'); 38 39 }else { 40 //タグの記事が無い場合の処理。記事が見つかりませんでしたみたいな。 41 //exit; 42 } 43 } 44 45帰ってきたデバッグ結果 46 47array( 48 (int) 0 => array( 49 'Photo' => array( 50 'id' => '14065', 51 'created' => '2018-01-16 20:19:26', 52 'dir' => '/files/aaa/', 53 'user_id' => '1', 54 'filename' => '/xxx.jpg', 55 ), 56 'User' => array( 57 'id' => '538', 58 'handlename' => '前原誠司', 59 'url' => '' 60 ), 61 (int) 0 => array( 62 'uniques' => '1' 63 ) 64 ) 65) 66 67UserテーブルのID=1は、安倍晋三にも関わらず、帰ってくるユーザーデータはUserテーブルからランダムなものが帰ってきてしまっています。 68
あなたの回答
tips
プレビュー