ユーザー情報を管理するUserテーブルと、つぶやき情報を管理するTweetテーブルがあります。
UserはhasmanyでTweetを持っています。
TweetはbelongsToでUserに属しています。
Search Pluginで検索した時に、ヒットした「つぶやき」を持つuser_idの他の「つぶやき」も参考として表示させたいと思っているのですが、方法が分かりません。
・Tweetの方からSearch pluginを使うことで、検索に該当したつぶやきのみは表示できていますが、同userの他のつぶやきを表示する方法が分かりません。(参考までに下記にソースを記載しています)
・Userの方からSearch pluginが使えればいける気もしましたが、hasmanyで持っているテーブル内を検索対象にする方法が分かりませんでした。
Tweet内でSearch pluginを使用 or User内でSearch pluginを使用、
実現したいことができれば、このどちらでも(もしくは他の方法でも)構わないのですが
良い方法がありましたらお教えいただけますと助かります。
何卒よろしくお願いします。
■cakeのバージョン:2.7
■データベース
Userテーブル(ユーザー)
ID|username 1 太郎 2 花子 ・ ・ Tweetテーブル(つぶやき)
ID | tweet | user_id
1 暑いなー 1
2 お腹いっぱい 1
3 楽しい! 1
4 お腹空いた 2
5 いい天気 2
6 風邪ひいた 2
7 嬉しい 2
■現状のソース(Tweetの方からSearch pluginを使用)
lang
1//Model/User.php 2<?php 3App::uses('AuthComponent', 'Controller/Component'); 4class User extends AppModel { 5 public $hasMany = array('Tweet'); 6?>
lang
1//Model/Tweet.php 2<?php 3class Tweet extends AppModel { 4 public $belongsTo = 'User'; 5 public $actsAs = array('Search.Searchable'); 6 public $filterArgs = array( 7 'tweet' => array('type' => 'like') 8 ); 9} 10?>
lang
1//Controller/TweetsController 2<?php 3 4class TweetsController extends AppController { 5 6 public $components = array('Search.Prg'); 7 public $presetVars = true; 8 public $paginate = array(); 9 10 public function search() { 11 $this->paginate = array( 12 'limit' => 100, 13 ); 14 $this->Prg->commonProcess(); 15 $this->paginate['conditions'] = $this->Tweet->parseCriteria($this->passedArgs); 16 $tweetLists = $this->paginate(); 17 $this->set(compact('tweetLists')); 18 } 19} 20?>
lang
1//View/Tweets/search.ctp 2<div> 3 <?php echo $this->Form->create('Tweet',array( 4 'novalidate' => true, 5 'url' => array_merge(array('action' => 'search'), 6 $this->params['pass']) 7 )); 8 ?> 9 <?php echo $this->Form->input('tweet', array( 10 'type' => 'text', 11 'placeholder' => '検索ワードを入力してください', 12 'label' => false 13 )); ?> 14 <?php echo $this->Form->submit('検索'); ?> 15 <?php echo $this->Form->end(); ?> 16 </div> 17 18 <div> 19 <h2>検索結果</h2> 20 <table> 21 <tr> 22 <th><?php echo $this->Paginator->sort('User.username', 'ユーザー名'); ?></th> 23 <th><?php echo $this->Paginator->sort('tweet', 'つぶやき'); ?></th> 24 </tr> 25 26 <?php foreach($tweetLists as $tweetList): ?> 27 28 <tr> 29 <td><?php echo $this->Html->link($tweetList['User']['username'],'/users/view/'.$tweetList['User']['id']) ?></td> 30 <td><?php echo h($tweetList['Tweet']['tweet']); ?></td> 31 </tr> 32 33 <?php endforeach; ?> 34 35 </table> 36 </div> 37 38 <div> 39 <?php echo $this->Paginator->prev(); ?> 40 <?php echo $this->Paginator->numbers(); ?> 41 <?php echo $this->Paginator->next(); ?> 42 </div>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/28 09:49