2012-07-06 37 views
0

我想用CakePHP建立一個Twitter克隆。我在我的模特之間建立了一個HABTM關係(如果我沒有正確執行此操作,我不會100%確定)。 我有我的數據庫3個表:CakePHP - 如何獲取用戶在Twitter克隆中關注的人的帖子?

  1. 用戶(ID,用戶名,密碼)
  2. 微博(ID,tweet_msg,USER_ID,創建)
  3. 關係(ID,USER_ID,follower_id)

我的模型設置如下:

用戶模型:

<?php 

    class User extends AppModel { 
    var $name = 'User'; 
    var $hasMany = 'Tweet'; 
    var $hasAndBelongsToMany = array(
      'Follower' => array(
        'className'    => 'Follower', 
        'joinTable'    => 'relationships', 
        'foreignKey'   => 'user_id', 
        'associationForeignKey' => 'follower_id' 
      ) 
    ); 

分享Tweet型號:

<?php 

class Tweet extends AppModel { 

    public $belongsTo = array(
     'User' => array(
      'className' => 'User', 
      'foreignKey' => 'user_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => 'Tweet.created DESC' 
     ) 
    ); 

} 

跟隨型號:

<?php 

class Follower extends AppModel{ 
     var $name  = 'Follower'; 
     var $useTable = 'users'; 
} 

假設我有HABTM關聯設置正確,我試圖讓人們的鳴叫用戶正在關注,但我不知道查詢應該是什麼樣子。我嘗試了一些方法來做到這一點,但他們都沒有工作。我認爲我不確定如何獲取用戶的以下用戶ID。我試過這樣的:$this->User->Follower->find('list');但我不知道我是否正確的方式。任何人都可以告訴我如何獲取用戶關注的人的推文嗎? 這將不勝感激, 謝謝。

回答

1

你必須讓你的用戶模型,例如:

<?php 

class User extends AppModel { 
    public $name = 'User'; 

    public $actsAs = array('Containable'); 

    public $hasMany = array(
     'Tweet' 
    ); 

    public $hasAndBelongsToMany = array(
     'Follower' => 
      array(
       'className'    => 'User', 
       'joinTable'    => 'relationships', 
       'foreignKey'   => 'user_id', 
       'associationForeignKey' => 'follower_id' 
      ) 
    ); 

} 

這樣做,這樣消除了多餘的跟隨模式的需要。接下來,在你的控制器嘗試這樣的事:

public function followerTweets($userid) { 
    $this->set('user', 
     $this->User->find('all', array(
     'contain' => array(
      'Follower' => array(
        'Tweet' 
      ) 
     ), 
     'conditions' => array(
      'User.id' => $userid 
     ) 
    ))); 
} 

然後,您可以通過這樣的觀點指定用戶的追隨者的鳴叫循環:

<?php 
$tweets = array(); 
foreach ($players['0']['Follower'] as $follower): 
$tweets = array_merge($follower['Tweet'], $tweets); 
endforeach; 
?> 

這給你留下一個數組,看起來像這樣:

var_dump($tweets) 

array 
    0 => 
    array 
     'id' => string '5' (length=1) 
     'user_id' => string '5' (length=1) 
    1 => 
    array 
     'id' => string '20' (length=2) 
     'user_id' => string '5' (length=1) 
    2 => 
    array 
     'id' => string '6' (length=1) 
     'user_id' => string '4' (length=1) 
    3 => 
    array 
     'id' => string '7' (length=1) 
     'user_id' => string '4' (length=1) 
    4 => 
    array 
     'id' => string '8' (length=1) 
     'user_id' => string '4' (length=1) 
    5 => 
    array 
     'id' => string '21' (length=2) 
     'user_id' => string '4' (length=1) 
    6 => 
    array 
     'id' => string '22' (length=2) 
     'user_id' => string '4' (length=1) 
    7 => 
    array 
     'id' => string '23' (length=2) 
     'user_id' => string '4' (length=1) 
    8 => 
    array 
     'id' => string '3' (length=1) 
     'user_id' => string '2' (length=1) 
    9 => 
    array 
     'id' => string '11' (length=2) 
     'user_id' => string '2' (length=1) 
    10 => 
    array 
     'id' => string '18' (length=2) 
     'user_id' => string '2' (length=1) 
    11 => 
    array 
     'id' => string '26' (length=2) 
     'user_id' => string '2' (length=1) 
相關問題