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:
- users (id, username, password)
- tweets (id, tweet_msg, user_id, created)
- 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.