This is an interesting scenario. You may want to customize the queries used by your model (using finderQuery) or just call $User->query() directly to fetch friend relationships in both directions.
If database table and index size isn't an issue though and you want to require friendships to always be mutual... I would recommend just adding two records to the mapping table for each instance. The somewhat redundant insertion and deletions would still probably reduce complexity overall. rhett waldock clearsite new media -----Original Message----- From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of thelorax Sent: Tuesday, March 06, 2007 2:41 PM To: Cake PHP Subject: Setting up a self-referential many-to-many relationship Hello, I just started playing with Cake yesterday and am trying to set up a self referential many to many relationship using an example of users/ friends that is common to most social networks. To set up the DB I created two tables; users with id and username, and friends with user_id and friend_id (both keys that reference users.id). As of now, I am just playing in scaffolding and have defined my user model as follows: <?php class User extends AppModel { var $name = 'User'; var $hasAndBelongsToMany = array('Friend' => array('className' => 'User', 'joinTable' => 'friends', 'foreignKey' => 'user_id', 'associationForeignKey' => 'friend_id', 'conditions' => '', 'order' => '', 'limit' => '', 'unique' => 'true', 'finderQuery' => '', 'deleteQuery' => '' ) ); } ?> Let's say I add a row in my friends table in which user_id = 1 and friend_id = 2. With this I am able to see a user 1's related friends in my scaffolding. However, when I view user 2's entry, I only see User 1 appear as a friend if I have created another row in my friends table in which user_id = user 2 and friend_id = user 1. I guess I could add two records as described above, each time people become friends, but it seems like it would be more efficient to in create one record and have the relationship go in both directions. The only problem is that I don't know how to set this up in cake. Any ideas? thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
