CakePHPでTwitterのクローンサイトを作成してます。
現状出来上がったものがユーザー検索機能結果に対して何回でも同じユーザーを登録できるなど、かなり欠陥のある機能になっています。ユーザー検索機能はうまく機能してますが、その横にただfollow unfollowのリンクが表示されているのみです。できれば検索の際にすでにフォロー済みか判断してフォロー済みならフォローボタンを表示せず、未フォローならフォローボタンを表示するようにしたいです。
そこで調べるとLeftJoinなどを使うと良いと出てくるのですが、どのように使えば良いかが分かりません。
かなり酷いコードだとは思いますが、上記機能を実現するためになるべく簡単にかけるコードなどをご教授頂けると幸いです。
UsersController.php public function find() { $userNameKeyword=$this->request->data['Users']['username']; $users=$this->User->find('all', [ 'conditions' => [ 'User.username LIKE' => '%'.$userNameKeyword.'%' ], ]); $this->set('users',$users); $this->render(); コード
FollowsController.php <?php class FollowsController extends AppController { public $_authentication = array('Posts','Post'); //const POST = 'Posts/post'; //const FOllOW = 'Follows/follow'; public function follow($following_id) { $id = $this->Auth->user('id'); $this->Follow->create(array('user_id'=> $id , 'following_id'=>$following_id)); if ($this->Follow->save()) { $this->redirect('/users/find'); $this->set('follow',$following_id); } } public function unfollow($following_id) { $this->Follow->delete($this->request->data('Follow.id')); } } コード
find.ctp <div class="row-fluid"> <div class="span9"> <table class="table"> <tr> <th><?php echo $this->Paginator->sort('id','ID');?></th> <th><?php echo $this->Paginator->sort('username','Username');?></th> <th><?php echo $this->Paginator->sort('follow','Follow');?></th> <th><?php echo $this->Paginator->sort('unfollow','Unfollow');?></th> </tr> <?php foreach($users as $key => $user):?> <tr> <td><?php echo h($user['User']['id']); ?></td> <td><?php echo $this->Html->link($user['User']['username'],'/follows/follow/' . $user['User']['id']);?></td> <td><?php echo $this->Html->link("follow",'/follows/follow/' . $user['User']['id']);?></td> <td><?php echo $this->Html->link("unollow",'/follows/unfollow/' . $user['User']['id']);?></td> </tr> <?php endforeach;?> </table> </div> <div class="span3"> <div class="well" style="margin-top:20px;"> <?php echo $this->Form->create('Users',array('url'=>'find')); ?> <fieldset> <legend>Search user</legend> </fieldset> <?php echo $this->Form->input('username', array('label' => 'Enter Username', 'class' => 'span12', 'empty' => true)); ?> <?php echo $this->Form->end('Search'); ?> </div> </div> </div> コード
あなたの回答
tips
プレビュー