2011-09-13 97 views
0

我在CakePHP中構建了一個使用兩個表的友誼系統:用戶和朋友。CakePHP友誼系統

在用戶(ID,用戶名,電子郵件,密碼)和朋友(ID,user_from,user_to,狀態)

用戶請求其他用戶成爲朋友,這會在朋友表中的記錄存儲兩個用戶ID並設置'請求'的狀態。用戶可以接受友誼,並將狀態更改爲「已接受」或取消友誼,並將記錄從數據庫中刪除。

一個例子鏈接請求樣子,既可以在用戶列表或用戶詳細信息頁面上顯示:

<?php echo $this->Html->link('Add as Friend', array('controller'=>'friends','action'=>'add_friend',$user['User']['id'])); ?>

問題1是我怎麼能做出這樣的鏈接變化取消請求鏈接,如果用戶有對他們的請求或已經是朋友?

此鏈接對應於控制器的以下方法:

function add_friend ($id) 
    { 
     if(!empty($this->data)) 
     { 
      $this->Friend->Create(); 
      if($this->Friend->save($this->data)) 
      { 
       $this->Session->setFlash('Friendship Requested'); 
       $this->redirect(array('controller'=>'users','action'=>'login')); 
      } 
     } 
    } 

所以我們傳遞ID,以這將是user_to然後將「user_from」需要該方法可以在用戶當前登錄並將狀態設置爲「請求」。 問題2是我該怎麼做?另外,如何通過只調用該方法來阻止用戶創建多個記錄,並顯示一條消息說您已請求友誼。

下一個方法是:

function accept_friendship ($id) 
    { 
    $this->Session->setFlash('Friendship Accepted'); 
       $this->redirect(array('controller'=>'friends','action'=>'index')); 
      } 
     } 
    } 

問題3:但我再次很困惑,我將如何改變記錄的狀態並標記爲用戶朋友,當該方法被調用。還需要防止在同一條記錄上多次調用此方法。

最後位列出朋友對於用戶或其他用戶:

function users_friends($id) 
{ 
$this->set('friends', $this->Friend->find('all')); 
} 

function my_friends() 
{ 
$this->set('friends', $this->Friend->find('all')); 
} 

正如你可以看到第一種方法需要您正在查看該用戶的ID,然後第二個方法將使用當前登錄的用戶ID。 問題4:我該如何使用它來列出該用戶的朋友?

如果任何人都可以幫助我把它放在正確的軌道上,這將是非常感謝,因爲我已經停下腳步,不知道如何做這4件事情,並試圖學習CakePHP盡我所能幫助非常感謝。謝謝

編輯:我發現隱藏字段的視圖可以用來存儲有關用戶確認的朋友請求的信息,但這不是理想的,因爲這意味着將用戶從其他地方發送出去時實際上我只想運行該函數並直接執行重定向。不要AJAX!

+0

有沒有更新?謝謝 – Cameron

+0

這應該真的被分解成單獨的問題 – Ross

回答

0

答案3是這樣的:

function accept_friendship ($id) { 
    $this->Friend->id = $id; 
    $current_status = $this->Friend->field('status'); 

    if($current_status=='Requested') { 
     $this->Application->saveField('status', 'Accepted'); 
    } 

    $this->Session->setFlash('Friendship Accepted'); 
    $this->redirect(array('controller'=>'friends','action'=>'index')); 
} 

基本上得到了朋友的ID請求,請檢查status字段,如果它等於Requested,然後更新到Accepted。這樣它只會被調用一次。

而且爲了防止人們反覆「接受」朋友,只要刪除「接受」鏈接一旦被接受。 if語句會阻止您的代碼不必要地更新。

你也應該把某種預防措施到位,這樣只有requested friend可以accept的要求。否則,我可以輸入網址yoursite.com/friends/accept_friendship/123,並接受一個隨機人員請求,而無需任何身份驗證。

+0

那麼我該如何做到這一點,你在最後提到的保護? – Cameron

1

答1和2:

function add_friend ($id) 
{ 
    if(!empty($this->data)) 
    { 
     $this->Friend->Create(); 
     if($this->Friend->save($this->data)) 
     { 
      $this->Session->setFlash('Friendship Requested'); 
      $this->redirect(array('controller'=>'users','action'=>'login')); 
     } 
    } 
if(empty($this->data)) 
    { 
     $this->set('friends', $this->Friend->find('all',array('Friend.id'=>$id)); 
    } 
} 




<?php 

if($friends['Friend']['status']=="Requested") 
{ 
    echo $this->Html->link('Request Pending', '#'); 
} 
else if($friends['Friend']['status']=="Accepted") 
{ 
    echo $this->Html->link('Already Friend', '#'); 
} 
else 
{ 
     echo $this->Html->link('Add as Friend', array('controller'=>'friends','action'=>'add_friend',$user['User']['id'])); 
} 
?> 

答3和4:

funcrion friendlist($user_id) 
{ 
$session_user_id = $this->Session->read('Auth.User.id') 
if($user_id == $session_user_id) 
{ 
    $user_to = $session_user_id ; 
} 
else 
{ 
    $user_to = $user_id; 
} 
$this->Friend->find('all',array('Friend.user_to'=>$user_to,'Friend.status'=>'Accepted') 
} 
+0

爲什麼你在add_friend方法中有一個find?這種方法應該是保存在表中的記錄,並沒有發現任何東西...:/ – Cameron