【问题标题】:CakePHP - How to get posts of people the user is following in Twitter Clone?CakePHP - 如何获取用户在 Twitter 克隆中关注的人的帖子?
【发布时间】:2012-07-06 18:29:42
【问题描述】:

我正在尝试使用 CakePHP 构建 Twitter 克隆。我在我的模型之间建立了一个 HABTM 关系(我不是 100% 确定我这样做是否正确)。 我的数据库中有 3 个表:

  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'
            )
      );

推特模特:

<?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-&gt;User-&gt;Follower-&gt;find('list'); 但我不知道我是否走对了路。谁能告诉我如何获取用户关注的人的推文? 这将不胜感激, 谢谢。

【问题讨论】:

    标签: php mysql cakephp twitter has-and-belongs-to-many


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-01
      • 2020-05-11
      • 2012-03-10
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多