After reviewing what you rewrote, I think I understand what you are doing. Your current structure will not work. POSTS has no friends links. Therefore, based on the pattern you posted, friends CANNOT add POSTS. I think that you are trying to link to a friend as one of the other users. Meaning, FRIEND users are actually just another USER in the USERS table. This is a HABTM standalone link. So here is what I would suggest:
1- First, make sure the HABTM table is created in the database:
- MySQL CREATE TABLE users_users ( user_id char (36) NOT NULL,
friend_id char (36) NOT NULL);
2- Establish relationships in the user model.
var $hasAndBelongsToMany = array( 'friend' => array('className' => 'User', 'joinTable' => 'users_users', 'foreignKey' => 'user_id', 'associationForeignKey' => 'friend_id', 'unique' => true, ), ); var $hasMany = array( 'Post' => array( 'className' => 'Post', 'foreignKey' => 'user_id' ), );
3 - use scaffolding to insert multiple posts, link to friends, and add posts. 4- Add a function to write a record to the Users controller:
function get_user($id) { $posts = $this->User->find('first', array( 'conditions' => array('User.id' => $id), 'recursive' => '2' )); pr($posts); }
5 Now you can query the User table using recursive to pull records using the following command:
http: // test / users / get_user / USER_ID
6- On your output all records will be displayed (recursively), including friends and their messages in the returned data tree, when you pr ($ posts)
I know this is a long post, but I think it will provide the best solution for what you are trying to do. The power of CakePHP is incredible. This is the learning curve that kills us.
Happy coding!