0

I am trying to build a Twitter Clone with CakePHP. I have a HABTM relationship set up between my models (I'm not a 100% sure if I did this correctly or not). I have 3 tables in my database:

  1. users (id, username, password)
  2. tweets (id, tweet_msg, user_id, created)
  3. relationships (id, user_id, follower_id)

My models are set up like this:

User Model:

<?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 Model:

<?php

class Tweet extends AppModel {

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

}

Follower Model:

<?php

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

Assuming that I have the HABTM association set up correctly, I am trying to get the Tweets of the people that the user is following, but I have no idea what the query is supposed to look like. I've tried a few ways to do this but none of them have worked. I think that I'm not sure how to get the User's following's ids. I tried something like: $this->User->Follower->find('list'); But I don't know if I'm going the right way or not. Can anyone tell me how to get the tweets of the people that the user is following? It would be greatly appreciated, Thank you.

user1302430
  • 342
  • 3
  • 6
  • 14

1 Answers1

1

You should set up your User model as such:

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

}

Doing it this way removes the need for the extra Follower model. Next in your controller try something like this:

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

You can then loop through the specified user's follower's tweets in the view like this:

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

This leaves you with an array that will look like this:

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)
Nick Savage
  • 856
  • 1
  • 7
  • 18