views:

221

answers:

1

Hi

I'm trying to set up a social networking type of site, where users can have friends and send each other messages.

I have a users table, and a friends table, which basically has an user_id and user_friends_id. Using cakePHP, how can I link the user_id and user_friend_id back to the users table? I used bake to create all my models and controllers, but when I viewed the users page, I got an error because cakePHP doesn't know how to link the user_friend_id back to the users table.

Can anyonw please help?

+2  A: 

table names (fields):

 users (user_id, user fields....)
 friends_users (user_id, friend_id)

associations:

 User hasAndBelongsToMany Friend
 Friend hasAndBelongsToMany User

in User model:

var $hasAndBelongsToMany = array(
    'Friend' => array(
        'className' => 'User',
        'joinTable' => 'friends_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'friend_id',
    );

in Friend model:

var $useTable = 'users';
var $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'friends_users',
        'foreignKey' => 'friend_id',
        'associationForeignKey' => 'user_id',
    );
Thank you very much.I didn't know about the'foreignKey' => 'friend_id', 'associationForeignKey' => 'user_id'I will give it a bash
Pierre